Skip to content

Commit

Permalink
Ensure that subclassed models have their own independent attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
shioyama committed Jun 15, 2017
1 parent ab0abbf commit c6c3a2c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/mobility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def translated_attribute_names
end

def inherited(subclass)
subclass.instance_variable_set(:@mobility, @mobility)
subclass.instance_variable_set(:@mobility, @mobility.dup)
super
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/mobility/wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,10 @@ def translated_attribute_names
def << backend_module
modules << backend_module
end

def initialize_dup(other)
@modules = modules.map(&:dup)
super
end
end
end
10 changes: 10 additions & 0 deletions spec/mobility_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ def attributes
subclass = Class.new(model)
expect(subclass.translated_attribute_names).to eq(["title"])
end

it "defines new translated attributes independently of superclass" do
model.include Mobility
model.translates :title, backend: :null
subclass = Class.new(model)
subclass.translates :content, backend: :null

expect(model.translated_attribute_names).to eq(["title"])
expect(subclass.translated_attribute_names).to match_array(["title", "content"])
end
end
end

Expand Down

0 comments on commit c6c3a2c

Please sign in to comment.