lean_tag 0.1.14 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5a7287a6748e83e628a9252001ede19ecf54367d
4
- data.tar.gz: 71387636c3f542a78d1b0954e6798e7f1df97fa0
3
+ metadata.gz: 7952206ef32a8fed9dd302b89f3a3aff2dd4ec53
4
+ data.tar.gz: 2a46bed46e47c6857016bf32e36ea6dd598d9b53
5
5
  SHA512:
6
- metadata.gz: 837aa758f9a4f40b9047835a2c7ab61fb6e89a1f27208324cd75aeb806435358d9e6b7e51f05c9a90b827ebae8bbebbfa328c53f6a0203abebdebb53c24f3766
7
- data.tar.gz: 477411f18fefc4779620c7ace59128eed2a1ffb648861d0ba0714fc9205d7024d23e8ee7d879dc19795b6ba0d8d9034268f2fd6e7f5c0c4ea52d43968209180d
6
+ metadata.gz: c385cf6fddf49d527b297fcefab95fde367b203ae8f7508893e584649bff04a7b7047bc43e4e779ebc81a7291ed2d5171c5047bf0e63249ef1ee2b27d2376cb1
7
+ data.tar.gz: 332a667a76d6146068c23c18161bbb59f2ee7ae89054b50497e216c3a57110dfea4699a0af05622f757587b94d0e0a2e313b7a295f0d11ce240a284a4631ad16
@@ -3,10 +3,10 @@ class CreateTaggings < ActiveRecord::Migration
3
3
  create_table :taggings do |t|
4
4
  t.references :tag, index: true
5
5
  t.references :record, polymorphic: true, index: true
6
-
6
+ t.string :filter, null: false, default: "tags"
7
7
  t.timestamps null: false
8
8
  end
9
9
 
10
- add_index [:record_type, :record_id, :filter]
10
+ add_index :taggings, [:record_type, :record_id, :filter]
11
11
  end
12
12
  end
data/lib/lean_tag/tag.rb CHANGED
@@ -4,7 +4,7 @@ module LeanTag
4
4
  self.table_name = :tags
5
5
 
6
6
  has_many :records, through: :taggings
7
- has_many :taggings, class_name: "LeanTag::Tagging", inverse_of: :tag
7
+ has_many :taggings, class_name: "LeanTag::Tagging", inverse_of: :tag, dependent: :destroy
8
8
 
9
9
  scope :matches, -> (list) { where("tags.name IN (?)", list) }
10
10
  scope :ranked, -> { order("taggings_count DESC") }
@@ -1,33 +1,50 @@
1
1
  module LeanTag
2
2
  module Taggable
3
3
 
4
- def self.included(base)
5
- base.extend ClassMethods
6
- base.send :include, InstanceMethods
4
+ def self.extended(base)
7
5
 
8
- base.class_eval do
9
- has_many :taggings, class_name: "LeanTag::Tagging", as: :record, inverse_of: :record, dependent: :destroy
10
- has_many :tags, through: :taggings
6
+ def taggable_on(relation="tags")
7
+ extend ClassMethods
11
8
 
12
- accepts_nested_attributes_for :taggings, allow_destroy: true
9
+ self.class_exec(relation) do |tag_relation|
10
+ include InstanceMethods
13
11
 
14
- scope :with_tags, -> { includes(:tags) }
12
+ has_many "#{tag_relation}_taggings".to_sym, -> { where(filter: tag_relation) }, class_name: "LeanTag::Tagging", as: :record, inverse_of: :record, dependent: :destroy
13
+ has_many tag_relation.to_sym, through: "#{tag_relation}_taggings".to_sym, source: :tag
14
+
15
+ accepts_nested_attributes_for "#{tag_relation}_taggings", allow_destroy: true
16
+
17
+ scope "with_#{tag_relation}", -> { includes(tag_relation) }
18
+
19
+ define_method "add_#{tag_relation.to_s.singularize}", ->(tag) { _add_tag(tag, tag_relation) }
20
+ define_method "add_#{tag_relation.to_s.singularize}!", ->(tag) { _add_tag!(tag, tag_relation) }
21
+ define_method "#{tag_relation.to_s.singularize}_list", ->() { _get_tag_list(tag_relation) }
22
+ define_method "remove_#{tag_relation.to_s.singularize}", ->(tag) { _remove_tag(tag, tag_relation) }
23
+ define_method "remove_#{tag_relation.to_s.singularize}!", ->(tag) { _remove_tag(tag, tag_relation) }
24
+ define_method "#{tag_relation.to_s.singularize}_list=", ->(list) { _set_tag_list(list, tag_relation) }
25
+ end
15
26
  end
27
+
16
28
  end
17
29
 
18
30
 
19
31
  module ClassMethods
20
32
 
21
- def tagged_in(list)
33
+ ##
34
+ # Returns all records which include at least one of the passed tags
35
+ def tagged_with(list, filter="tags")
22
36
  list = list.split(LeanTag.config.delimiter) if list.is_a?(String)
23
37
  tag_ids = Tag.matches(list).pluck(:id)
24
- record_ids = Tagging.where(record_type: self.name, tag_id: tag_ids).pluck(:record_id).uniq
25
- return self.where(id: record_ids)
38
+ taggings = Tagging.where(record_type: self.name, tag_id: tag_ids).where(filter: filter)
39
+ return self.where(id: taggings.select(:record_id).distinct)
26
40
  end
27
41
 
28
- def tags
29
- tag_ids = Tagging.where(record_type: self.name).pluck(:tag_id).uniq
30
- return Tag.where(id: tag_ids)
42
+
43
+ ##
44
+ # Get all tags used by this class
45
+ def tags(filter="tags")
46
+ taggings = Tagging.where(record_type: self.name, filter: filter).where(filter: filter)
47
+ return Tag.where(id: taggings.select(:tag_id).distinct)
31
48
  end
32
49
 
33
50
  end
@@ -37,76 +54,75 @@ module LeanTag
37
54
 
38
55
  ##
39
56
  # Adds a single tag on parent save
40
- def add_tag(tag)
57
+ def _add_tag(tag, filter="tags")
41
58
  if tag.is_a?(String)
42
59
  record = Tag.find_by_name(tag)
43
60
  end
44
61
 
45
62
  if record.nil?
46
- self.tags.build(name: tag)
47
- elsif !self.taggings.exists?(tag_id: record.id)
48
- self.taggings.build(tag_id: record.id)
63
+ self._tags(filter).build(name: tag)
64
+ elsif !self._taggings(filter).exists?(tag_id: record.id)
65
+ self._taggings(filter).build(tag_id: record.id)
49
66
  end
50
67
  end
51
68
 
52
69
  ##
53
70
  # Adds a single tag immediately
54
- def add_tag!(tag)
55
- self.add_tag(tag)
71
+ def _add_tag!(tag, filter="tags")
72
+ self._add_tag(tag, filter)
56
73
  self.save!
57
74
  end
58
75
 
59
76
  ##
60
- # Finds current tags on this record which aren't in the passed list
61
- def excluded_tags(tag_names)
62
- self.tags.reject { |t| t.name.in?(tag_names) }
63
- end
64
-
65
- ##
66
- # Finds current tags on this record which are in the passed list
67
- def included_tags(tag_names)
68
- self.tags.select { |t| t.name.in?(tag_names) }
77
+ # Returns a delimited list of tag names
78
+ def _get_tag_list(filter="tags")
79
+ self._taggings(filter).map(&:tag).map(&:name).join(',')
69
80
  end
70
81
 
71
- ##
72
82
  # Removes a single tag on parent save
73
- def remove_tag(tag)
83
+ def _remove_tag(tag, filter="tags")
74
84
  if tag.is_a?(String)
75
- tag = self.tags.where(name: tag).first
85
+ tag = Tag.find_by_name(tag)
76
86
  end
77
87
 
78
- self.taggings.each { |t| t.mark_for_destruction if t.tag.eql?(tag) }
88
+ self._taggings(filter).each { |t| t.mark_for_destruction if t.tag.eql?(tag) }
79
89
  end
80
90
 
81
91
  ##
82
92
  # Removes a single tag immediately
83
- def remove_tag!(tag)
84
- self.remove_tag(tag)
93
+ def _remove_tag!(tag, filter="tags")
94
+ self._remove_tag(tag, filter)
85
95
  self.save!
86
96
  end
87
97
 
88
98
  ##
89
99
  # Set a list of tags
90
- def tag_list=(value)
91
- if value.is_a?(String)
92
- tag_names = value.split(LeanTag.config.delimiter)
93
- elsif value.is_a?(Array)
94
- tag_names = value
100
+ def _set_tag_list(list, filter="tags")
101
+ if list.is_a?(String)
102
+ tag_names = list.split(LeanTag.config.delimiter)
103
+ elsif list.is_a?(Array)
104
+ tag_names = list
95
105
  else
96
106
  tag_names = []
97
107
  end
98
108
 
99
- # Get rid of existing tags that aren't in the list
100
- self.excluded_tags(tag_names).each { |t| self.remove_tag(t) }
109
+ tags = Tag.where(id: self._taggings(filter).select(:tag_id).distinct)
110
+ tags.reject { |t| t.name.in?(tag_names) }.each { |t| self._remove_tag(t, filter) }
101
111
 
102
112
  # Add any new tags
103
- tag_names.each { |t| self.add_tag(t) }
113
+ tag_names.each { |t| self._add_tag(t, filter) }
104
114
  end
105
115
 
106
116
  ##
107
- # Returns a delimited list of tag names
108
- def tag_list
109
- self.tags.map(&:name).join(',')
117
+ # Gets relevant tag association
118
+ def _tags(filter)
119
+ self.send(filter)
120
+ end
121
+
122
+ ##
123
+ # Gets relevant taggings association
124
+ def _taggings(filter)
125
+ self.send("#{filter}_taggings")
110
126
  end
111
127
 
112
128
  end
@@ -4,7 +4,9 @@ module LeanTag
4
4
  self.table_name = :taggings
5
5
 
6
6
  belongs_to :record, polymorphic: true, inverse_of: :taggings
7
- belongs_to :tag, class_name: "LeanTag::Tag", inverse_of: :taggings, counter_cache: :taggings_count
7
+ belongs_to :tag, class_name: "LeanTag::Tag", inverse_of: :taggings, counter_cache: :taggings_count, autosave: true
8
+
9
+ scope :for, -> (filter) { where(filter: filter) }
8
10
 
9
11
  validates :record_id, presence: true
10
12
  validates :record_type, presence: true
@@ -1,5 +1,5 @@
1
1
  module LeanTag
2
2
 
3
- VERSION = '0.1.14'
3
+ VERSION = '1.0.0'
4
4
 
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lean_tag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.14
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Ellis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-26 00:00:00.000000000 Z
11
+ date: 2015-04-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec