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

Dash defaults not working with ActiveSupport's as_json #362

Closed
joevandyk opened this issue Jul 30, 2016 · 7 comments
Closed

Dash defaults not working with ActiveSupport's as_json #362

joevandyk opened this issue Jul 30, 2016 · 7 comments
Labels

Comments

@joevandyk
Copy link
Contributor

joevandyk commented Jul 30, 2016

I'm not sure if this is a Hashie or ActiveSupport problem. I would expect fields to be an array, not a hash when as_json is used. It works fine if I don't use a proc for the default.

require 'active_support'
require 'active_support/core_ext'

class DashTestDefaultProc < Hashie::Dash
  property :fields, default: -> { [] }
end

describe DashTestDefaultProc, :focus do
  it do
    object = described_class.new
    expect(object.as_json).to be == { "fields" => [] }
  end
end
DashTestDefaultProc
  should be == {"fields"=>[]} (FAILED - 1)

Failures:

  1) DashTestDefaultProc should be == {"fields"=>[]}
     Failure/Error: expect(object.as_json).to be == { "fields" => [] }
       expected: == {"fields"=>[]}
            got:    {"fields"=>{}}
@dblock
Copy link
Member

dblock commented Jul 31, 2016

Looks legit. You should PR a (failing) spec.

@joevandyk
Copy link
Contributor Author

joevandyk commented Aug 1, 2016

done in #363

@sazor
Copy link
Contributor

sazor commented Oct 19, 2016

I found what causes this problem. Proc is evaluated first time only when property is directly called. #as_json method from active_support doesn't get values but just goes through hash using #map and call as_json for each value. So it tries to call as_json in Proc, it isn't defined and because of it uses as_json of Object which leads to collect its instance_variable and return {} since there is no variables. I see two ways of solving it: evaluate proc in time of initialization or monkey patch something (add as_json method to Proc or do something with ActiveSupport patches).

@dblock
Copy link
Member

dblock commented Oct 19, 2016

I'm against monkey patching FWIW ;)

@sazor
Copy link
Contributor

sazor commented Oct 20, 2016

Maybe default procs should be evaluated when object is initialized? Method #values also returns procs and i think it's weird because #[] and #values should return same values.

@dblock
Copy link
Member

dblock commented Oct 20, 2016

I am not sure @sazor, try to PR something and we can see what specs that breaks ;)

@dblock
Copy link
Member

dblock commented Oct 31, 2016

Closed via #380.

@dblock dblock closed this as completed Oct 31, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants