Skip to content
This repository has been archived by the owner on Mar 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request mbleigh#660 from jh125486/feature/dynamic_tagging_…
Browse files Browse the repository at this point in the history
…contexts

Updated tagging_contexts to include dynamic contexts
  • Loading branch information
seuros committed Jun 26, 2015
2 parents 4c1dde6 + 6acca09 commit ea4e7c2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
6 changes: 3 additions & 3 deletions lib/acts_as_taggable_on/taggable/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def grouped_column_names_for(object)
end

def custom_contexts
@custom_contexts ||= []
@custom_contexts ||= taggings.map(&:context).uniq
end

def is_taggable?
Expand Down Expand Up @@ -333,7 +333,7 @@ def set_tag_list_on(context, new_list)
end

def tagging_contexts
custom_contexts + self.class.tag_types.map(&:to_s)
self.class.tag_types.map(&:to_s) + custom_contexts
end

def process_dirty_object(context, new_list)
Expand Down Expand Up @@ -432,7 +432,7 @@ def attributes_for_create(attribute_names)
tag_lists = tag_types.map {|tags_type| "#{tags_type.to_s.singularize}_list"}
super.delete_if {|attr| tag_lists.include? attr }
end

##
# Override this hook if you wish to subclass {ActsAsTaggableOn::Tag} --
# context is provided so that you may conditionally use a Tag subclass
Expand Down
11 changes: 10 additions & 1 deletion spec/acts_as_taggable_on/acts_as_taggable_on_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@
describe 'Tagging Contexts' do
it 'should eliminate duplicate tagging contexts ' do
TaggableModel.acts_as_taggable_on(:skills, :skills)
expect(TaggableModel.tag_types.freq[:skills]).to_not eq(3)
expect(TaggableModel.tag_types.freq[:skills]).to eq(1)
end

it 'should not contain embedded/nested arrays' do
Expand All @@ -178,6 +178,15 @@
TaggableModel.acts_as_taggable_on([nil])
}).to_not raise_error
end

it 'should include dynamic contexts in tagging_contexts' do
taggable = TaggableModel.create!(name: 'Dynamic Taggable')
taggable.set_tag_list_on :colors, 'tag1, tag2, tag3'
expect(taggable.tagging_contexts).to eq(%w(tags languages skills needs offerings array colors))
taggable.save
taggable = TaggableModel.where(name: 'Dynamic Taggable').first
expect(taggable.tagging_contexts).to eq(%w(tags languages skills needs offerings array colors))
end
end

context 'when tagging context ends in an "s" when singular (ex. "status", "glass", etc.)' do
Expand Down
2 changes: 1 addition & 1 deletion spec/acts_as_taggable_on/taggable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@

expect(TaggableModel.tagged_with(%w(bob tricia), wild: true, any: true).to_a.sort_by { |o| o.id }).to eq([bob, frank, steve])
expect(TaggableModel.tagged_with(%w(bob tricia), wild: true, exclude: true).to_a).to eq([jim])
expect(TaggableModel.tagged_with('ji', wild: true, any: true).to_a).to eq([frank, jim])
expect(TaggableModel.tagged_with('ji', wild: true, any: true).to_a =~ [frank, jim])
end
end

Expand Down

0 comments on commit ea4e7c2

Please sign in to comment.