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

Property renamed with :from is not set #10

Closed
chrisupb opened this issue Jan 15, 2012 · 16 comments
Closed

Property renamed with :from is not set #10

chrisupb opened this issue Jan 15, 2012 · 16 comments

Comments

@chrisupb
Copy link

My user model has the property :password_confirmation and includes the UserRepresenter.

My UserRepresenter defines

property :password_confirmation, :from => :passwordConfirmation

I do a POST with HTTP body:
{
"passwordConfirmation":"123"
}

My controller:

def create
 @user = User.new.from_json(request.raw_post)
 puts @user.password_confirmation # =====> ""
 puts @user.passwordConfirmation  # ==== => "123"
end

Why is password_confirmation not set?

@apotonick
Copy link
Member

Looks like a bug, but can you double-check if you're up-to-date, etc? Please write a provoking test, but check this first: https://github.com/apotonick/representable/blob/master/test/json_test.rb#L263

@chrisupb
Copy link
Author

What I could find out: This is a Mongoid issue!
If I don't include the representer but extend the class it's working.

Another interesting fact: I include several Mongoid classes in my user class.

class User
 include Roar::Representer
 include UserRepresenter
 include Mongoid::Document
 include Mongoid::Paranoia
 include Mongoid::Timestamps
 include Mongoid::Slug

If I don't include the Representer BEFORE the Mongoid includes:
=> wrong number of arguments (0 for 1)

representable (1.0.1) lib/representable.rb:124:in `collection'
mongoid (2.4.0) lib/mongoid/contexts/mongo.rb:8:in `collection'
mongoid (2.4.0) lib/mongoid/contexts/mongo.rb:93:in `count'
mongoid (2.4.0) lib/mongoid/criteria.rb:155:in `exists?'
mongoid (2.4.0) lib/mongoid/validations/uniqueness.rb:68:in `validate_each'
activemodel (3.1.0) lib/active_model/validator.rb:153:in `block in validate'
activemodel (3.1.0) lib/active_model/validator.rb:150:in `each'

But I gotta sleep now...

@apotonick
Copy link
Member

Well, then Mongoid overwrites the #collection class method. Solution: Don't include the representer module but extend your objects at runtime, this doesn't pollute your class.

@chrisupb
Copy link
Author

It's not an general issue, as if you only include Mongoid::Document its working.

I will close this issue for now and perhaps write about a solution later.

@chrisupb
Copy link
Author

Hmm, it's getting worse with Mongoid. I removed all Roar includes from my model classes as my PolymorphicExtender test seems to work now, but then extending embedded objects doesn't work:

class User
include Mongoid::Document
  embeds_one :profile, :class_name => "Users::Profile"
end

class Users::Profile
include Mongoid::Document
  field :name
end    

user = User.find(id)
respond_with user.profile.extend(ProfileRepresenter)

Extending fails with undefined method `representable_attrs' for Users::Profile:Class

@apotonick
Copy link
Member

Paste your ProfileRepresenter!

@chrisupb
Copy link
Author

Nothing special :)

module ProfileRepresenter
   include Roar::Representer::JSON

   property :name
end

@apotonick
Copy link
Member

@chrisupb Are you sure you updated both Roar and representable to the latest version?

@chrisupb
Copy link
Author

Yeah I double checked it. OK I use roar-rails but it fetches
roar 0.9.1
representable 1.0.1
roar-rails (0.0.2)

No change If I use my local repos for the 3 projects.

I already came up against this problem when I first used roar one month ago. But I could fix it by including roar stuff in my models.

I think this is a hint, that extending doesn't work:
https://github.com/apotonick/representable/blob/master/lib/representable.rb#L39 (notice the puts)

  def self.extended(object)

    attrs = representable_attrs
    object.instance_eval do
      @representable_attrs = attrs
    end
    puts object.representable_attrs
  end

Error for nested profile: undefined method `representable_attrs' for #Users::Profile:0x000000050a8ac0

"Error" for normal property: private method `representable_attrs' called for #User:0x00000004913058

So an User IS extended but not the embedded profile.

@apotonick
Copy link
Member

Ah, ok if you have nested objects that need a separate representer you have to use the :extend option: http://nicksda.apotomo.de/2011/12/ruby-on-rest-2-representers-and-the-dci-pattern/

Does that help?

@chrisupb
Copy link
Author

No because with "nested" I mean Mongoid embedded in this context.
I ONLY use this simple ProfileRepresenter but for extending an in an user embedded profile class

@apotonick
Copy link
Member

OOoook, now I get it. Can you provide a minimal app setup so I can investigate that - seems like a misunderstanding between Mongoid and Representable, maybe the #profile method doesn't return a real Profile object but some proxy or something. Thx!

@chrisupb
Copy link
Author

Here it is: https://github.com/chrisupb/mongoid-sample
See README
Thank you!

@chrisupb chrisupb reopened this Feb 3, 2012
@chrisupb
Copy link
Author

chrisupb commented Feb 3, 2012

I reopen this one, as it's really a show blocker for me.

@apotonick
Copy link
Member

Fixed in trailblazer/representable@b7eceac (1.1.1). Some strange mongoid issue that wouldn't let us set instance variables. Was ugly code anyway, so, thanks :-D Also thanks for providing a minimal example, I could very easily setup a test and fix it: trailblazer/representable@b7eceac#diff-2

@chrisupb
Copy link
Author

chrisupb commented Feb 7, 2012

Awesome!! Thank you. This is working now without including anything into the models.

Unfortunately URL helpers are no longer available in the embedded class (instrument) undefined local variable or method `artists_url' for #<Artists::Instrument:0x00000001c16000

I updated my mongoid-example for this problem but did not yet examine it.

undergroundwebdesigns pushed a commit to smile-io/representable that referenced this issue Jul 3, 2019
…able_attrs ivar directly but use a setter. this makes it work with mongoid and fixes trailblazer/roar#10.
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

2 participants