Module: ActsAsTaggableOn::Taggable::Ownership
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/acts-as-taggable-on/taggable/ownership.rb
Instance Method Summary collapse
- #cached_owned_tag_list_on(context) ⇒ Object
- #owner_tag_list_on(owner, context) ⇒ Object
- #owner_tags(owner) ⇒ Object
- #owner_tags_on(owner, context) ⇒ Object
- #reload(*args) ⇒ Object
- #save_owned_tags ⇒ Object
- #set_owner_tag_list_on(owner, context, new_list) ⇒ Object
Instance Method Details
#cached_owned_tag_list_on(context) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/acts-as-taggable-on/taggable/ownership.rb', line 60 def cached_owned_tag_list_on(context) variable_name = "@owned_#{context}_list" (instance_variable_defined?(variable_name) && instance_variable_get(variable_name)) || instance_variable_set( variable_name, {} ) end |
#owner_tag_list_on(owner, context) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/acts-as-taggable-on/taggable/ownership.rb', line 67 def owner_tag_list_on(owner, context) add_custom_context(context) cache = cached_owned_tag_list_on(context) cache[owner] ||= ActsAsTaggableOn::TagList.new(*(owner, context).map(&:name)) end |
#owner_tags(owner) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/acts-as-taggable-on/taggable/ownership.rb', line 31 def (owner) scope = if owner.nil? else .where( ActsAsTaggableOn::Tagging.table_name.to_s => { tagger_id: owner.id, tagger_type: owner.class.base_class.to_s } ) end # when preserving tag order, return tags in created order # if we added the order to the association this would always apply if self.class.preserve_tag_order? scope.order("#{ActsAsTaggableOn::Tagging.table_name}.id") else scope end end |
#owner_tags_on(owner, context) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/acts-as-taggable-on/taggable/ownership.rb', line 52 def (owner, context) (owner).where( ActsAsTaggableOn::Tagging.table_name.to_s => { context: context } ) end |
#reload(*args) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/acts-as-taggable-on/taggable/ownership.rb', line 83 def reload(*args) self.class.tag_types.each do |context| instance_variable_set("@owned_#{context}_list", nil) end super(*args) end |
#save_owned_tags ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/acts-as-taggable-on/taggable/ownership.rb', line 91 def tagging_contexts.each do |context| cached_owned_tag_list_on(context).each do |owner, tag_list| # Find existing tags or create non-existing tags: = (tag_list.uniq, context) # Tag objects for owned tags = (owner, context).to_a # 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 |= .from(index) |= .from(index) & # Order the array of tag objects to match the tag list = .map do |t| .find do |n| n.name.downcase == t.name.downcase end end.compact end else # Delete discarded tags and create new tags end # Find all taggings that belong to the taggable (self), are owned by the owner, # have the correct context, and are removed from the list. if .present? ActsAsTaggableOn::Tagging.where(taggable_id: id, taggable_type: self.class.base_class.to_s, tagger_type: owner.class.base_class.to_s, tagger_id: owner.id, tag_id: , context: context).destroy_all end # Create new taggings: .each do |tag| taggings.create!(tag_id: tag.id, context: context.to_s, tagger: owner, taggable: self) end end end true end |
#set_owner_tag_list_on(owner, context, new_list) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/acts-as-taggable-on/taggable/ownership.rb', line 75 def set_owner_tag_list_on(owner, context, new_list) add_custom_context(context) cache = cached_owned_tag_list_on(context) cache[owner] = ActsAsTaggableOn.default_parser.new(new_list).parse end |