lean_tag 1.0.1 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/lean_tag/engine.rb +2 -0
- data/lib/lean_tag/tag.rb +3 -1
- data/lib/lean_tag/taggable.rb +17 -7
- data/lib/lean_tag/tagging.rb +2 -1
- data/lib/lean_tag/version.rb +3 -1
- data/lib/lean_tag.rb +7 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f369a5cfbafb5c23c6842f54d5d4559fa7f66569
|
4
|
+
data.tar.gz: 8f603c5cd549aaae3d8fe1b6cd955eb1c48341ec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f765bdb09d95df190fddc8c54c12bf074368f24ad7ac10ced9338a6e6896c25eea9a04629d01fed5d0606c41248511afd0cd72e2bf2673f726c278926cfe9749
|
7
|
+
data.tar.gz: 4129c3c05ba79ee0d85faad922d520cffcbe7a51d31d1f924c16212bb04c4c15f8fdaa4ed1eb00d6906b0b88dda9ab2090fc152cd269f8b3c9fd4ab6c3599620
|
data/lib/lean_tag/engine.rb
CHANGED
data/lib/lean_tag/tag.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
module LeanTag
|
2
2
|
class Tag < ActiveRecord::Base
|
3
|
+
|
3
4
|
self.table_name = :tags
|
4
5
|
|
5
6
|
has_many :records, through: :taggings
|
@@ -12,9 +13,10 @@ module LeanTag
|
|
12
13
|
|
13
14
|
def name=(value)
|
14
15
|
if value.present?
|
15
|
-
self[:name] = value.gsub(
|
16
|
+
self[:name] = value.gsub(LeanTag.config.legal_characters, "")
|
16
17
|
self[:name] = self[:name].downcase if LeanTag.config.force_lowercase
|
17
18
|
end
|
18
19
|
end
|
20
|
+
|
19
21
|
end
|
20
22
|
end
|
data/lib/lean_tag/taggable.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
module LeanTag
|
2
2
|
module Taggable
|
3
|
+
|
3
4
|
def self.extended(base)
|
5
|
+
|
4
6
|
def taggable_on(relation="tags")
|
5
7
|
extend ClassMethods
|
6
8
|
|
@@ -18,13 +20,16 @@ module LeanTag
|
|
18
20
|
define_method "add_#{tag_relation.to_s.singularize}!", ->(tag) { _add_tag!(tag, tag_relation) }
|
19
21
|
define_method "#{tag_relation.to_s.singularize}_list", ->() { _get_tag_list(tag_relation) }
|
20
22
|
define_method "remove_#{tag_relation.to_s.singularize}", ->(tag) { _remove_tag(tag, tag_relation) }
|
21
|
-
define_method "remove_#{tag_relation.to_s.singularize}!", ->(tag) { _remove_tag
|
23
|
+
define_method "remove_#{tag_relation.to_s.singularize}!", ->(tag) { _remove_tag(tag, tag_relation) }
|
22
24
|
define_method "#{tag_relation.to_s.singularize}_list=", ->(list) { _set_tag_list(list, tag_relation) }
|
23
25
|
end
|
24
26
|
end
|
27
|
+
|
25
28
|
end
|
26
29
|
|
30
|
+
|
27
31
|
module ClassMethods
|
32
|
+
|
28
33
|
##
|
29
34
|
# Returns all records which include at least one of the passed tags
|
30
35
|
def tagged_with(list, filter="tags")
|
@@ -34,25 +39,27 @@ module LeanTag
|
|
34
39
|
return self.where(id: taggings.select(:record_id).distinct)
|
35
40
|
end
|
36
41
|
|
42
|
+
|
37
43
|
##
|
38
44
|
# Get all tags used by this class
|
39
45
|
def tags(filter="tags")
|
40
46
|
taggings = Tagging.where(record_type: self.name, filter: filter).where(filter: filter)
|
41
47
|
return Tag.where(id: taggings.select(:tag_id).distinct)
|
42
48
|
end
|
49
|
+
|
43
50
|
end
|
44
51
|
|
52
|
+
|
45
53
|
module InstanceMethods
|
54
|
+
|
46
55
|
##
|
47
56
|
# Adds a single tag on parent save
|
48
57
|
def _add_tag(tag, filter="tags")
|
49
58
|
if tag.is_a?(String)
|
50
|
-
record = Tag.
|
59
|
+
record = Tag.find_or_create_by(name: tag)
|
51
60
|
end
|
52
61
|
|
53
|
-
|
54
|
-
self._tags(filter).build(name: tag)
|
55
|
-
elsif !self._taggings(filter).exists?(tag_id: record.id)
|
62
|
+
unless self._taggings(filter).exists?(tag_id: record.id)
|
56
63
|
self._taggings(filter).build(tag_id: record.id)
|
57
64
|
end
|
58
65
|
end
|
@@ -97,8 +104,9 @@ module LeanTag
|
|
97
104
|
tag_names = []
|
98
105
|
end
|
99
106
|
|
100
|
-
tags =
|
101
|
-
tags.reject { |t| t.name.in?(tag_names) }
|
107
|
+
tags = self._taggings(filter).map(&:tag)
|
108
|
+
tags.reject! { |t| t.name.in?(tag_names) }
|
109
|
+
tags.each { |t| self._remove_tag(t, filter) }
|
102
110
|
|
103
111
|
# Add any new tags
|
104
112
|
tag_names.each { |t| self._add_tag(t, filter) }
|
@@ -115,6 +123,8 @@ module LeanTag
|
|
115
123
|
def _taggings(filter)
|
116
124
|
self.send("#{filter}_taggings")
|
117
125
|
end
|
126
|
+
|
118
127
|
end
|
128
|
+
|
119
129
|
end
|
120
130
|
end
|
data/lib/lean_tag/tagging.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
module LeanTag
|
2
2
|
class Tagging < ActiveRecord::Base
|
3
|
+
|
3
4
|
self.table_name = :taggings
|
4
5
|
|
5
6
|
belongs_to :record, polymorphic: true, inverse_of: :taggings
|
@@ -7,8 +8,8 @@ module LeanTag
|
|
7
8
|
|
8
9
|
scope :for, -> (filter) { where(filter: filter) }
|
9
10
|
|
10
|
-
validates :record_id, presence: true
|
11
11
|
validates :record_type, presence: true
|
12
12
|
validates :tag, presence: true, uniqueness: { scope: [:record_type, :record_id] }
|
13
|
+
|
13
14
|
end
|
14
15
|
end
|
data/lib/lean_tag/version.rb
CHANGED
data/lib/lean_tag.rb
CHANGED
@@ -5,6 +5,7 @@ require 'active_support/core_ext/module'
|
|
5
5
|
require_relative 'lean_tag/engine' if defined?(Rails)
|
6
6
|
|
7
7
|
module LeanTag
|
8
|
+
|
8
9
|
def self.config
|
9
10
|
@configuration ||= Configuration.new
|
10
11
|
end
|
@@ -14,13 +15,18 @@ module LeanTag
|
|
14
15
|
yield self.config if block_given?
|
15
16
|
end
|
16
17
|
|
18
|
+
|
17
19
|
class Configuration
|
18
|
-
|
20
|
+
|
21
|
+
attr_accessor :delimiter, :force_lowercase, :legal_characters, :remove_unused
|
19
22
|
|
20
23
|
def initialize
|
21
24
|
self.delimiter = ','
|
22
25
|
self.force_lowercase = true
|
26
|
+
self.legal_characters = /[^0-9a-zA-Z]+/
|
23
27
|
self.remove_unused = true
|
24
28
|
end
|
29
|
+
|
25
30
|
end
|
31
|
+
|
26
32
|
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: 1.0.
|
4
|
+
version: 1.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Ellis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-04-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -103,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
103
103
|
version: '0'
|
104
104
|
requirements: []
|
105
105
|
rubyforge_project:
|
106
|
-
rubygems_version: 2.
|
106
|
+
rubygems_version: 2.4.4
|
107
107
|
signing_key:
|
108
108
|
specification_version: 4
|
109
109
|
summary: A lightweight method for tagging content
|