lean_tag 0.1.5 → 0.1.13

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: 5c4c9dab55f5a421f093dab3d2aa54b068e2d979
4
- data.tar.gz: d561913360f1b7225d23cf44f24f584b788947f7
3
+ metadata.gz: 2dd657758a95c3cf60c8a8c4b114a611cca14c63
4
+ data.tar.gz: d71ef2552be6a0b7b529ab8d26935b718dc82b91
5
5
  SHA512:
6
- metadata.gz: a1f765cc88d1bcf0c317dfb21ca12e150b51c34e97101ccc0a93cd3a50a0b9d228631a4d2420e4e09994c5f9c9f8c2b8a378e4614223af042698e53b4f9c0d9d
7
- data.tar.gz: 9f987131d6426194ba05e5d4fb5c3fe681a6da5ea6b69226a0795ed25c9e74e44f4f697482c601be3bb9e1eb9a763969921a1029e698c41adf1b0c09ffc6a5a3
6
+ metadata.gz: ae98ad3da770f5c2453c3ec6beaba0da04687022cb00316dabfa69d0df8c72599ae148e15a102a09fdb0691e979b98f2a980bc5bc85bd139a7c3e1044ec579eb
7
+ data.tar.gz: f833b97b9ca7e45d8dd4c42dbec0ece5cfc7a9e73a4d355c302bd902a6c90275b0e1064f6ffc0045170f58c8438e6dbfaacb05bfc54da29c697b6eb1fb0608ba
data/lib/lean_tag/tag.rb CHANGED
@@ -6,6 +6,7 @@ module LeanTag
6
6
  has_many :records, through: :taggings
7
7
  has_many :taggings, class_name: "LeanTag::Tagging", inverse_of: :tag
8
8
 
9
+ scope :matches, -> (list) { where("tags.name IN (?)", list) }
9
10
  scope :ranked, -> { order("taggings_count DESC") }
10
11
 
11
12
  validates :name, presence: true, uniqueness: true
@@ -2,6 +2,9 @@ module LeanTag
2
2
  module Taggable
3
3
 
4
4
  def self.included(base)
5
+ base.extend ClassMethods
6
+ base.send :include, InstanceMethods
7
+
5
8
  base.class_eval do
6
9
  has_many :taggings, class_name: "LeanTag::Tagging", as: :record, inverse_of: :record, dependent: :destroy
7
10
  has_many :tags, through: :taggings
@@ -12,90 +15,100 @@ module LeanTag
12
15
  end
13
16
  end
14
17
 
15
- ##
16
- # Adds a single tag on parent save
17
- def add_tag(tag)
18
- if tag.is_a?(String)
19
- tag_name = tag
20
- tag = Tag.find_by_name(tag_name)
21
-
22
- if tag.nil?
23
- self.tags.build(name: tag_name)
24
- elsif !self.taggings.exists?(tag_id: tag.id)
25
- self.taggings.build(tag_id: tag.id)
26
- end
27
- else
28
- self.taggings.build(tag_id: tag.id)
18
+
19
+ module ClassMethods
20
+
21
+ def tagged_in(list)
22
+ list = list.split(LeanTag.config.delimiter) if list.is_a?(String)
23
+ 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)
29
26
  end
30
- end
31
27
 
32
- ##
33
- # Adds a single tag immediately
34
- def add_tag!(tag)
35
- if tag.is_a?(String)
36
- tag_name = tag
37
- tag = Tag.find_by_name(tag_name)
38
-
39
- if tag.nil?
40
- self.tags.create(name: tag_name)
41
- elsif !self.taggings.exists?(tag_id: tag.id)
42
- self.taggings.create(tag_id: tag.id)
43
- end
44
- else
45
- self.taggings.create(tag_id: tag.id)
28
+ def tags
29
+ tag_ids = Tagging.where(record_type: self.name).pluck(:tag_id).uniq
30
+ return Tag.where(id: tag_ids)
46
31
  end
47
- end
48
32
 
49
- ##
50
- # Finds current tags on this record which aren't in the passed list
51
- def excluded_tags(tag_names)
52
- self.tags.reject { |t| t.name.in?(tag_names) }
53
33
  end
54
34
 
55
- ##
56
- # Finds current tags on this record which are in the passed list
57
- def included_tags(tag_names)
58
- self.tags.select { |t| t.name.in?(tag_names) }
59
- end
60
35
 
61
- ##
62
- # Removes a single tag on parent save
63
- def remove_tag(tag)
64
- tag = self.tags.where(name: tag).first if tag.is_a?(String)
36
+ module InstanceMethods
65
37
 
66
- self.taggings.each { |t| t.mark_for_destruction if t.tag.eql?(tag) }
67
- end
38
+ ##
39
+ # Adds a single tag on parent save
40
+ def add_tag(tag)
41
+ if tag.is_a?(String)
42
+ record = Tag.find_by_name(tag)
43
+ end
68
44
 
69
- ##
70
- # Removes a single tag immediately
71
- def remove_tag!(tag)
72
- tag = self.tags.where(name: tag).first if tag.is_a?(String)
45
+ 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)
49
+ end
50
+ end
73
51
 
74
- self.taggings.each { |t| t.mark_for_destruction if t.tag.eql?(tag) }
75
- end
52
+ ##
53
+ # Adds a single tag immediately
54
+ def add_tag!(tag)
55
+ self.add_tag(tag)
56
+ self.save!
57
+ end
76
58
 
77
- ##
78
- # Set a list of tags
79
- def tag_list=(value)
80
- if value.is_a?(String)
81
- tag_names = value.split(LeanTag.config.delimiter)
82
- elsif value.is_a?(Array)
83
- tag_names = value
84
- else
85
- tag_names = []
59
+ ##
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) }
86
63
  end
87
64
 
88
- # Get rid of existing tags that aren't in the list
89
- self.excluded_tags(tag_names).each { |t| self.remove_tag(t) }
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) }
69
+ end
90
70
 
91
- # Add any new tags
92
- tag_names.each { |t| self.add_tag(t) }
93
- end
71
+ ##
72
+ # Removes a single tag on parent save
73
+ def remove_tag(tag)
74
+ if tag.is_a?(String)
75
+ tag = self.tags.where(name: tag).first
76
+ end
77
+
78
+ self.taggings.each { |t| t.mark_for_destruction if t.tag.eql?(tag) }
79
+ end
80
+
81
+ ##
82
+ # Removes a single tag immediately
83
+ def remove_tag!(tag)
84
+ self.remove_tag(tag)
85
+ self.save!
86
+ end
87
+
88
+ ##
89
+ # 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
95
+ else
96
+ tag_names = []
97
+ end
98
+
99
+ # Get rid of existing tags that aren't in the list
100
+ self.excluded_tags(tag_names).each { |t| self.remove_tag(t) }
101
+
102
+ # Add any new tags
103
+ tag_names.each { |t| self.add_tag(t) }
104
+ end
105
+
106
+ ##
107
+ # Returns a delimited list of tag names
108
+ def tag_list
109
+ self.tags.map(&:name).join(',')
110
+ end
94
111
 
95
- ##
96
- # Returns a delimited list of tag names
97
- def tag_list
98
- self.tags.map(&:name).join(',')
99
112
  end
100
113
 
101
114
  end
@@ -1,5 +1,5 @@
1
1
  module LeanTag
2
2
 
3
- VERSION = '0.1.5'
3
+ VERSION = '0.1.13'
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.5
4
+ version: 0.1.13
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-19 00:00:00.000000000 Z
11
+ date: 2015-04-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec