Module: ActsAsTaggableOn::Taggable::Core::InstanceMethods
- Defined in:
- lib/acts_as_taggable_on/acts_as_taggable_on/core.rb
Instance Method Summary collapse
- #add_custom_context(value) ⇒ Object
- #all_tags_list_on(context) ⇒ Object
-
#all_tags_on(context) ⇒ Object
Returns all tags of a given context.
- #cached_tag_list_on(context) ⇒ Object
- #custom_contexts ⇒ Object
-
#grouped_column_names_for(object) ⇒ Object
all column names are necessary for PostgreSQL group clause.
- #is_taggable? ⇒ Boolean
- #process_dirty_object(context, new_list) ⇒ Object
- #reload(*args) ⇒ Object
- #save_tags ⇒ Object
- #set_tag_list_on(context, new_list) ⇒ Object
- #tag_list_cache_on(context) ⇒ Object
- #tag_list_cache_set_on(context) ⇒ Object
- #tag_list_on(context) ⇒ Object
- #tagging_contexts ⇒ Object
-
#tags_on(context) ⇒ Object
Returns all tags that are not owned of a given context.
Instance Method Details
#add_custom_context(value) ⇒ Object
208 209 210 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 208 def add_custom_context(value) custom_contexts << value.to_s unless custom_contexts.include?(value.to_s) or self.class.tag_types.map(&:to_s).include?(value.to_s) end |
#all_tags_list_on(context) ⇒ Object
231 232 233 234 235 236 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 231 def (context) variable_name = "@all_#{context.to_s.singularize}_list" return instance_variable_get(variable_name) if instance_variable_get(variable_name) instance_variable_set(variable_name, ActsAsTaggableOn::TagList.new((context).map(&:name)).freeze) end |
#all_tags_on(context) ⇒ Object
Returns all tags of a given context
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 240 def (context) tag_table_name = ActsAsTaggableOn::Tag.table_name tagging_table_name = ActsAsTaggableOn::Tagging.table_name opts = ["#{tagging_table_name}.context = ?", context.to_s] scope = .where(opts) if ActsAsTaggableOn::Tag.using_postgresql? group_columns = grouped_column_names_for(ActsAsTaggableOn::Tag) scope = scope.order("max(#{tagging_table_name}.created_at)").group(group_columns) else scope = scope.group("#{ActsAsTaggableOn::Tag.table_name}.#{ActsAsTaggableOn::Tag.primary_key}") end scope.all end |
#cached_tag_list_on(context) ⇒ Object
212 213 214 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 212 def cached_tag_list_on(context) self["cached_#{context.to_s.singularize}_list"] end |
#custom_contexts ⇒ Object
200 201 202 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 200 def custom_contexts @custom_contexts ||= [] end |
#grouped_column_names_for(object) ⇒ Object
all column names are necessary for PostgreSQL group clause
196 197 198 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 196 def grouped_column_names_for(object) self.class.grouped_column_names_for(object) end |
#is_taggable? ⇒ Boolean
204 205 206 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 204 def is_taggable? self.class.is_taggable? end |
#process_dirty_object(context, new_list) ⇒ Object
280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 280 def process_dirty_object(context,new_list) value = new_list.is_a?(Array) ? new_list.join(', ') : new_list attrib = "#{context.to_s.singularize}_list" if changed_attributes.include?(attrib) # The attribute already has an unsaved change. old = changed_attributes[attrib] changed_attributes.delete(attrib) if (old.to_s == value.to_s) else old = tag_list_on(context).to_s changed_attributes[attrib] = old if (old.to_s != value.to_s) end end |
#reload(*args) ⇒ Object
294 295 296 297 298 299 300 301 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 294 def reload(*args) self.class.tag_types.each do |context| instance_variable_set("@#{context.to_s.singularize}_list", nil) instance_variable_set("@all_#{context.to_s.singularize}_list", nil) end super(*args) end |
#save_tags ⇒ Object
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 303 def tagging_contexts.each do |context| next unless tag_list_cache_set_on(context) # List of currently assigned tag names tag_list = tag_list_cache_on(context).uniq # Find existing tags or create non-existing tags: = ActsAsTaggableOn::Tag.find_or_create_all_with_like_by_name(tag_list) # Tag objects for currently assigned tags = (context) # Tag maintenance based on whether preserving the created order of tags if self.class.preserve_tag_order? # First off order the array of tag objects to match the tag list # rather than existing tags followed by new tags = tag_list.map{|l| .detect{|t| t.name.downcase == l.downcase}} # To preserve tags in the order in which they were added # delete all current tags and create new tags if the content or order has changed = ( == ? [] : ) = ( == ? [] : ) else # Delete discarded tags and create new tags = - = - end # Find taggings to remove: if .present? old_taggings = taggings.where(:tagger_type => nil, :tagger_id => nil, :context => context.to_s, :tag_id => ).all end # Destroy old taggings: if old_taggings.present? ActsAsTaggableOn::Tagging.destroy_all "#{ActsAsTaggableOn::Tagging.primary_key}".to_sym => old_taggings.map(&:id) end # Create new taggings: .each do |tag| taggings.create!(:tag_id => tag.id, :context => context.to_s, :taggable => self) end end true end |
#set_tag_list_on(context, new_list) ⇒ Object
267 268 269 270 271 272 273 274 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 267 def set_tag_list_on(context, new_list) add_custom_context(context) variable_name = "@#{context.to_s.singularize}_list" process_dirty_object(context, new_list) unless custom_contexts.include?(context.to_s) instance_variable_set(variable_name, ActsAsTaggableOn::TagList.from(new_list)) end |
#tag_list_cache_on(context) ⇒ Object
221 222 223 224 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 221 def tag_list_cache_on(context) variable_name = "@#{context.to_s.singularize}_list" instance_variable_get(variable_name) || instance_variable_set(variable_name, ActsAsTaggableOn::TagList.new((context).map(&:name))) end |
#tag_list_cache_set_on(context) ⇒ Object
216 217 218 219 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 216 def tag_list_cache_set_on(context) variable_name = "@#{context.to_s.singularize}_list" !instance_variable_get(variable_name).nil? end |
#tag_list_on(context) ⇒ Object
226 227 228 229 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 226 def tag_list_on(context) add_custom_context(context) tag_list_cache_on(context) end |
#tagging_contexts ⇒ Object
276 277 278 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 276 def tagging_contexts custom_contexts + self.class.tag_types.map(&:to_s) end |
#tags_on(context) ⇒ Object
Returns all tags that are not owned of a given context
259 260 261 262 263 264 265 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 259 def (context) scope = .where(["#{ActsAsTaggableOn::Tagging.table_name}.context = ? AND #{ActsAsTaggableOn::Tagging.table_name}.tagger_id IS NULL", context.to_s]) # when preserving tag order, return tags in created order # if we added the order to the association this would always apply scope = scope.order("#{ActsAsTaggableOn::Tagging.table_name}.id") if self.class.preserve_tag_order? scope.all end |