Skip to content

Commit

Permalink
allow parse_root_in_json's format option to be inherited
Browse files Browse the repository at this point in the history
  • Loading branch information
agrobbin committed Nov 1, 2013
1 parent 55334c3 commit 339c31c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
40 changes: 17 additions & 23 deletions lib/her/model/parse.rb
Expand Up @@ -41,15 +41,13 @@ def to_params(attributes, changes={})
# include Her::Model
# include_root_in_json true
# end
def include_root_in_json(value = nil)
@_her_include_root_in_json ||= begin
superclass.include_root_in_json if superclass.respond_to?(:include_root_in_json)
end

return @_her_include_root_in_json unless value
def include_root_in_json(value)
@_her_include_root_in_json = value
end
alias include_root_in_json? include_root_in_json

def include_root_in_json?
@_her_include_root_in_json || (superclass.respond_to?(:include_root_in_json?) && superclass.include_root_in_json?)
end

# Return or change the value of `parse_root_in`
#
Expand All @@ -58,16 +56,14 @@ def include_root_in_json(value = nil)
# include Her::Model
# parse_root_in_json true
# end
def parse_root_in_json(value = nil, options = {})
@_her_parse_root_in_json ||= begin
superclass.parse_root_in_json if superclass.respond_to?(:parse_root_in_json)
end

return @_her_parse_root_in_json unless value
def parse_root_in_json(value, options = {})
@_her_parse_root_in_json = value
@_her_parse_root_in_json_format = options[:format]
end
alias parse_root_in_json? parse_root_in_json

def parse_root_in_json?
@_her_parse_root_in_json || (superclass.respond_to?(:parse_root_in_json?) && superclass.parse_root_in_json?)
end

# Return or change the value of `request_new_object_on_build`
#
Expand All @@ -77,14 +73,12 @@ def parse_root_in_json(value = nil, options = {})
# request_new_object_on_build true
# end
def request_new_object_on_build(value = nil)
@_her_request_new_object_on_build ||= begin
superclass.request_new_object_on_build if superclass.respond_to?(:request_new_object_on_build)
end

return @_her_request_new_object_on_build unless value
@_her_request_new_object_on_build = value
end
alias request_new_object_on_build? request_new_object_on_build

def request_new_object_on_build?
@_her_request_new_object_on_build || (superclass.respond_to?(:request_new_object_on_build?) && superclass.request_new_object_on_build?)
end

# Return or change the value of `root_element`. Always defaults to the base name of the class.
#
Expand Down Expand Up @@ -139,17 +133,17 @@ def pluralized_parsed_root_element

# @private
def included_root_element
include_root_in_json == true ? root_element : include_root_in_json
include_root_in_json? == true ? root_element : include_root_in_json?
end

# @private
def parsed_root_element
parse_root_in_json == true ? root_element : parse_root_in_json
parse_root_in_json? == true ? root_element : parse_root_in_json?
end

# @private
def active_model_serializers_format?
@_her_parse_root_in_json_format == :active_model_serializers
@_her_parse_root_in_json_format == :active_model_serializers || (superclass.respond_to?(:active_model_serializers_format?) && superclass.active_model_serializers_format?)
end
end
end
Expand Down
8 changes: 7 additions & 1 deletion spec/model/parse_spec.rb
Expand Up @@ -135,9 +135,10 @@ class User < Foo::Model; end
before do
Her::API.default_api.connection.adapter :test do |stub|
stub.post("/users") { |env| [200, {}, { :user => { :id => 1, :fullname => "Lindsay Fünke" } }.to_json] }
stub.get("/users") { |env| [200, {}, { :users => [ { :id => 1, :fullname => "Lindsay Fünke" } ] }.to_json] }
end

spawn_model("Foo::Model") { parse_root_in_json true }
spawn_model("Foo::Model") { parse_root_in_json true, format: :active_model_serializers }
class User < Foo::Model
collection_path "/users"
end
Expand All @@ -149,6 +150,11 @@ class User < Foo::Model
@new_user = User.create(:fullname => "Lindsay Fünke")
@new_user.fullname.should == "Lindsay Fünke"
end

it "parses the collection of data" do
@users = User.all
@users.first.fullname.should == "Lindsay Fünke"
end
end

context "to true with :format => :active_model_serializers" do
Expand Down

0 comments on commit 339c31c

Please sign in to comment.