Module: ActsAsTaggableOn::Taggable::Core
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/acts-as-taggable-on/taggable/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
-
#load_tags(tag_list) ⇒ Object
Find existing tags or create non-existing tags.
- #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
- #taggable_tenant ⇒ 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
136 137 138 139 140 |
# File 'lib/acts-as-taggable-on/taggable/core.rb', line 136 def add_custom_context(value) unless custom_contexts.include?(value.to_s) || self.class.tag_types.map(&:to_s).include?(value.to_s) custom_contexts << value.to_s end end |
#all_tags_list_on(context) ⇒ Object
167 168 169 170 171 172 173 174 |
# File 'lib/acts-as-taggable-on/taggable/core.rb', line 167 def (context) variable_name = "@all_#{context.to_s.singularize}_list" if instance_variable_defined?(variable_name) && instance_variable_get(variable_name) return instance_variable_get(variable_name) end 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
178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/acts-as-taggable-on/taggable/core.rb', line 178 def (context) tagging_table_name = ActsAsTaggableOn::Tagging.table_name opts = ["#{tagging_table_name}.context = ?", context.to_s] scope = .where(opts) if ActsAsTaggableOn::Utils.using_postgresql? group_columns = grouped_column_names_for(ActsAsTaggableOn::Tag) scope.order(Arel.sql("max(#{tagging_table_name}.created_at)")).group(group_columns) else scope.group("#{ActsAsTaggableOn::Tag.table_name}.#{ActsAsTaggableOn::Tag.primary_key}") end.to_a end |
#cached_tag_list_on(context) ⇒ Object
142 143 144 |
# File 'lib/acts-as-taggable-on/taggable/core.rb', line 142 def cached_tag_list_on(context) self["cached_#{context.to_s.singularize}_list"] end |
#custom_contexts ⇒ Object
128 129 130 |
# File 'lib/acts-as-taggable-on/taggable/core.rb', line 128 def custom_contexts @custom_contexts ||= taggings.map(&:context).uniq end |
#grouped_column_names_for(object) ⇒ Object
all column names are necessary for PostgreSQL group clause
124 125 126 |
# File 'lib/acts-as-taggable-on/taggable/core.rb', line 124 def grouped_column_names_for(object) self.class.grouped_column_names_for(object) end |
#is_taggable? ⇒ Boolean
132 133 134 |
# File 'lib/acts-as-taggable-on/taggable/core.rb', line 132 def is_taggable? self.class.is_taggable? end |
#load_tags(tag_list) ⇒ Object
Find existing tags or create non-existing tags
233 234 235 |
# File 'lib/acts-as-taggable-on/taggable/core.rb', line 233 def (tag_list) ActsAsTaggableOn::Tag.find_or_create_all_with_like_by_name(tag_list) end |
#reload(*args) ⇒ Object
222 223 224 225 226 227 228 229 |
# File 'lib/acts-as-taggable-on/taggable/core.rb', line 222 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
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/acts-as-taggable-on/taggable/core.rb', line 237 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: = (tag_list, context) # Tag objects for currently assigned tags = (context) # Tag maintenance based on whether preserving the created order of tags = - = - if self.class.preserve_tag_order? = & if .any? && [0....size] != index = .each_with_index do |_, i| break i unless [i] == [i] end # Update arrays of tag objects |= [index....size] |= [index....size] & # Order the array of tag objects to match the tag list = .map do |t| .find { |n| n.name.downcase == t.name.downcase } end.compact end else # Delete discarded tags and create new tags end # Destroy old taggings: taggings.not_owned.by_context(context).where(tag_id: ).destroy_all if .present? # Create new taggings: .each do |tag| if taggable_tenant taggings.create!(tag_id: tag.id, context: context.to_s, taggable: self, tenant: taggable_tenant) else taggings.create!(tag_id: tag.id, context: context.to_s, taggable: self) end end end true end |
#set_tag_list_on(context, new_list) ⇒ Object
204 205 206 207 208 209 210 211 212 |
# File 'lib/acts-as-taggable-on/taggable/core.rb', line 204 def set_tag_list_on(context, new_list) add_custom_context(context) variable_name = "@#{context.to_s.singularize}_list" parsed_new_list = ActsAsTaggableOn.default_parser.new(new_list).parse instance_variable_set(variable_name, parsed_new_list) end |
#tag_list_cache_on(context) ⇒ Object
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/acts-as-taggable-on/taggable/core.rb', line 151 def tag_list_cache_on(context) variable_name = "@#{context.to_s.singularize}_list" if instance_variable_get(variable_name) instance_variable_get(variable_name) elsif cached_tag_list_on(context) && ensure_included_cache_methods! && self.class.caching_tag_list_on?(context) instance_variable_set(variable_name, ActsAsTaggableOn.default_parser.new(cached_tag_list_on(context)).parse) else instance_variable_set(variable_name, ActsAsTaggableOn::TagList.new((context).map(&:name))) end end |
#tag_list_cache_set_on(context) ⇒ Object
146 147 148 149 |
# File 'lib/acts-as-taggable-on/taggable/core.rb', line 146 def tag_list_cache_set_on(context) variable_name = "@#{context.to_s.singularize}_list" instance_variable_defined?(variable_name) && instance_variable_get(variable_name) end |
#tag_list_on(context) ⇒ Object
162 163 164 165 |
# File 'lib/acts-as-taggable-on/taggable/core.rb', line 162 def tag_list_on(context) add_custom_context(context) tag_list_cache_on(context) end |
#taggable_tenant ⇒ Object
218 219 220 |
# File 'lib/acts-as-taggable-on/taggable/core.rb', line 218 def taggable_tenant public_send(self.class.tenant_column) if self.class.tenant_column end |
#tagging_contexts ⇒ Object
214 215 216 |
# File 'lib/acts-as-taggable-on/taggable/core.rb', line 214 def tagging_contexts self.class.tag_types.map(&:to_s) + custom_contexts end |
#tags_on(context) ⇒ Object
Returns all tags that are not owned of a given context
194 195 196 197 198 199 200 201 202 |
# File 'lib/acts-as-taggable-on/taggable/core.rb', line 194 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 end |