lean_tag 0.1.14 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/db/migrate/2_create_taggings.rb +2 -2
- data/lib/lean_tag/tag.rb +1 -1
- data/lib/lean_tag/taggable.rb +62 -46
- data/lib/lean_tag/tagging.rb +3 -1
- data/lib/lean_tag/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7952206ef32a8fed9dd302b89f3a3aff2dd4ec53
|
4
|
+
data.tar.gz: 2a46bed46e47c6857016bf32e36ea6dd598d9b53
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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") }
|
data/lib/lean_tag/taggable.rb
CHANGED
@@ -1,33 +1,50 @@
|
|
1
1
|
module LeanTag
|
2
2
|
module Taggable
|
3
3
|
|
4
|
-
def self.
|
5
|
-
base.extend ClassMethods
|
6
|
-
base.send :include, InstanceMethods
|
4
|
+
def self.extended(base)
|
7
5
|
|
8
|
-
|
9
|
-
|
10
|
-
has_many :tags, through: :taggings
|
6
|
+
def taggable_on(relation="tags")
|
7
|
+
extend ClassMethods
|
11
8
|
|
12
|
-
|
9
|
+
self.class_exec(relation) do |tag_relation|
|
10
|
+
include InstanceMethods
|
13
11
|
|
14
|
-
|
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
|
-
|
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
|
-
|
25
|
-
return self.where(id:
|
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
|
-
|
29
|
-
|
30
|
-
|
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
|
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.
|
47
|
-
elsif !self.
|
48
|
-
self.
|
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
|
55
|
-
self.
|
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
|
-
#
|
61
|
-
def
|
62
|
-
self.
|
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
|
83
|
+
def _remove_tag(tag, filter="tags")
|
74
84
|
if tag.is_a?(String)
|
75
|
-
tag =
|
85
|
+
tag = Tag.find_by_name(tag)
|
76
86
|
end
|
77
87
|
|
78
|
-
self.
|
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
|
84
|
-
self.
|
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
|
91
|
-
if
|
92
|
-
tag_names =
|
93
|
-
elsif
|
94
|
-
tag_names =
|
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
|
-
|
100
|
-
|
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.
|
113
|
+
tag_names.each { |t| self._add_tag(t, filter) }
|
104
114
|
end
|
105
115
|
|
106
116
|
##
|
107
|
-
#
|
108
|
-
def
|
109
|
-
self.
|
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
|
data/lib/lean_tag/tagging.rb
CHANGED
@@ -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
|
data/lib/lean_tag/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2015-04-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|