Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can't override instance methods with Inheritance #509

Open
masciugo opened this issue Sep 3, 2018 · 3 comments
Open

can't override instance methods with Inheritance #509

masciugo opened this issue Sep 3, 2018 · 3 comments

Comments

@masciugo
Copy link

masciugo commented Sep 3, 2018

in my use case I have several Her model so I created the following super class

  class HerModel
    include Her::JsonApi::Model

    def created_at
      @created_at ||= Time.zone.parse(super)
    end

  end

and classes like this

class CollectorContent < HerModel
  use_api COLLECTOR_CONTENTS_WS_API
  collection_path "collector/:collector_id/collector_contents"
  type :collector_contents
end

In order to have the CollectorContent created_at attribute overridden I need to define it in every child class:

class CollectorContent < HerModel
  use_api COLLECTOR_CONTENTS_WS_API
  collection_path "collector/:collector_id/collector_contents"
  type :collector_contents

  def created_at
    @created_at ||= Time.zone.parse(super)
  end

end

am I missing anything?

@evgeniy-kunitsa
Copy link

faced with the same situation, is there any information?

@edtjones
Copy link
Collaborator

@masciugo @evgeniy-kunitsa is this peculiar to the created_at attribute, or any attribute you define in the base class?

Does it work if you define it in a mixin instead?

@evgeniy-kunitsa
Copy link

@edtjones, no, in my case I have base class:

module Api
 class Organization
   include Her::Model

   def suborganizations
     @suborganizations ||= self[:suborganizations].map { |org| Organization.new(org) }
   end
 end
end

And child class:

module External
  class Organization < Api::Organization
  end
end

I try to do smth like this:

Api::Organization.new(suborganizations: [{ uid: 123123 }]).suborganizations
=> [#<Api::Organization(organizations) uid=123123>]

External::Organization.new(suborganizations: [{ uid: 123123 }]).suborganizations
=> [{"uid"=>123123}]

Why in second case I got an array of hashes instead of array of organizations?

FYI: ruby 2.5.3, rails 5.2.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants