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

Updated tagging_contexts to include dynamic contexts #660

Merged
merged 2 commits into from
Jun 26, 2015
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
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