Skip to content

Commit

Permalink
modify relation to check for the class_name option first before const…
Browse files Browse the repository at this point in the history
…antizing the acts_as name
  • Loading branch information
Rob Jacoby committed Jul 31, 2014
1 parent a8a00fe commit 72a8e40
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/active_record/acts_as/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def acts_as(name, scope = nil, options = {})

cattr_reader(:acting_as_reflection) { reflections[name.to_sym] }
cattr_reader(:acting_as_name) { name.to_s }
cattr_reader(:acting_as_model) { name.to_s.camelize.constantize }
cattr_reader(:acting_as_model) { (options[:class_name] || name.to_s.camelize).constantize }
class_eval "def acting_as() #{name} || build_#{name} end"
alias_method :acting_as=, "#{name}=".to_sym

Expand Down
19 changes: 19 additions & 0 deletions spec/acts_as_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
expect(association.options).to have_key(:as)
end

it "has a cattr_reader for the acting_as_model" do
expect(subject.acting_as_model).to eq Product
end

describe "#acting_as?" do
it "returns true for supermodel class and name" do
expect(Pen.acting_as? :product).to be true
Expand Down Expand Up @@ -180,4 +184,19 @@
expect(relation.scope_for_create[:name]).to eq('new name')
end
end

context 'Namespaces' do
subject { Inventory::PenLid }

it "has a has_one relation" do
association = subject.reflect_on_all_associations.find { |r| r.name == :product_feature }
expect(association).to_not be_nil
expect(association.macro).to eq(:has_one)
expect(association.options).to have_key(:as)
end

it "has a cattr_reader for the acting_as_model" do
expect(subject.acting_as_model).to eq Inventory::ProductFeature
end
end
end

0 comments on commit 72a8e40

Please sign in to comment.