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

Add proper STI support #43

Merged
merged 3 commits into from
Jun 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/mobility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def define_join_method(association_name, translation_class)
relation.joins(m.join(t, join_type).
on(t[:key].eq(attribute).
and(t[:locale].eq(Mobility.locale).
and(t[:translatable_type].eq(name).
and(t[:translatable_type].eq(base_class.name).
and(t[:translatable_id].eq(m[:id]))))).join_sources)
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 = other.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
10 changes: 10 additions & 0 deletions spec/support/shared_examples/querying_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@
end
end
end

context "with single table inheritance" do
let(:sti_model) { Class.new(model_class) }

it "works with sti model" do
instance = sti_model.create(attribute1 => "foo")
sti_model.i18n.where(attribute1 => "foo")
expect(sti_model.i18n.where(attribute1 => "foo")).to match_array([instance])
end
end
end

describe ".not" do
Expand Down