Module: ActsAsTaggable::HelperMethods

Defined in:
lib/acts_as_taggable/acts_as_taggable.rb

Class Method Summary collapse

Class Method Details

.current_tag_scopeObject

Returns the current tag scope, e.g. :tag_type => ‘material’



186
187
188
# File 'lib/acts_as_taggable/acts_as_taggable.rb', line 186

def self.current_tag_scope
  Tag.current_scope.where_values_hash if Tag.current_scope
end

.filter_tags_by_current_tag_scope(tags) ⇒ Object

Filters an array of tags by the current tag scope



168
169
170
171
172
173
174
175
# File 'lib/acts_as_taggable/acts_as_taggable.rb', line 168

def self.filter_tags_by_current_tag_scope(tags)
  return tags unless current_tag_scope
  tags.select do |tag|
    current_tag_scope.all? do |attribute, value|
      tag[attribute].to_s == value.to_s
    end
  end
end

.scope_class_methods(metaclass, tag_type) ⇒ Object



139
140
141
142
143
144
145
146
147
148
# File 'lib/acts_as_taggable/acts_as_taggable.rb', line 139

def self.scope_class_methods(metaclass, tag_type)
  scope_tag_method(metaclass, tag_type, :create_tag, "create_#{tag_type}_tag")
  scope_tag_method(metaclass, tag_type, :find_tags, "find_#{tag_type}_tags")
  scope_tag_method(metaclass, tag_type, :tagged_with_any, "tagged_with_any_#{tag_type}")
  scope_tag_method(metaclass, tag_type, :tagged_with_all, "tagged_with_all_#{tag_type.to_s.pluralize}")
  scope_tag_method(metaclass, tag_type, :tags, "#{tag_type}_tags")
  scope_tag_method(metaclass, tag_type, :tag_names, "#{tag_type}_tag_names")
  scope_tag_method(metaclass, tag_type, :applied_tags, "applied_#{tag_type}_tags")
  scope_tag_method(metaclass, tag_type, :applied_tag_names, "applied_#{tag_type}_tag_names")
end

.scope_instance_methods(klass, tag_type) ⇒ Object



150
151
152
153
154
155
156
157
# File 'lib/acts_as_taggable/acts_as_taggable.rb', line 150

def self.scope_instance_methods(klass, tag_type)
  scope_tag_method(klass, tag_type, :tag_names, "#{tag_type}_tag_names")
  scope_tag_method(klass, tag_type, :tag_names=, "#{tag_type}_tag_names=")
  scope_tag_method(klass, tag_type, :tag_string, "#{tag_type}_tag_string")
  scope_tag_method(klass, tag_type, :tag_string=, "#{tag_type}_tag_string=")
  scope_tag_method(klass, tag_type, :tag_with, "tag_with_#{tag_type}")
  scope_tag_method(klass, tag_type, :untag_with, "untag_with_#{tag_type}")
end

.scope_tag_method(context, tag_type, method_name, scoped_method_name) ⇒ Object



159
160
161
162
163
164
165
# File 'lib/acts_as_taggable/acts_as_taggable.rb', line 159

def self.scope_tag_method(context, tag_type, method_name, scoped_method_name)
  context.send :define_method, scoped_method_name do |*args|
    Tag.where(:tag_type => tag_type).scoping do
      send(method_name, *args)
    end
  end
end

.scoped_association_assignment_nameObject



181
182
183
# File 'lib/acts_as_taggable/acts_as_taggable.rb', line 181

def self.scoped_association_assignment_name
  current_tag_scope ? "#{current_tag_scope['tag_type']}_tags=" : "tags="
end

.scoped_association_nameObject



177
178
179
# File 'lib/acts_as_taggable/acts_as_taggable.rb', line 177

def self.scoped_association_name
  current_tag_scope ? "#{current_tag_scope['tag_type']}_tags" : "tags"
end