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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9a5597de96b7f9ec49591cf11c3d4c4969d2beb1
4
- data.tar.gz: 88328d047bad479c91c3249181be49decee783d9
3
+ metadata.gz: f369a5cfbafb5c23c6842f54d5d4559fa7f66569
4
+ data.tar.gz: 8f603c5cd549aaae3d8fe1b6cd955eb1c48341ec
5
5
  SHA512:
6
- metadata.gz: ab0269cf7dc8a631806289b29a1f3713c71e19a55ca0fcd43be9b22ec6c6ec98961b425cd2b723e7a7e8fc6dcd99ebfe1d2553d7ced573cf079a7c9bc9b38e3e
7
- data.tar.gz: 13dafdafb371590755c1d77d04d6e8ecd501fcc88b0c15954729dea98dd13e003b2afd3ba9b751d2a7d8e371dc57b3b7f7d612503e934c000ef37149ea05db7b
6
+ metadata.gz: f765bdb09d95df190fddc8c54c12bf074368f24ad7ac10ced9338a6e6896c25eea9a04629d01fed5d0606c41248511afd0cd72e2bf2673f726c278926cfe9749
7
+ data.tar.gz: 4129c3c05ba79ee0d85faad922d520cffcbe7a51d31d1f924c16212bb04c4c15f8fdaa4ed1eb00d6906b0b88dda9ab2090fc152cd269f8b3c9fd4ab6c3599620
@@ -6,6 +6,8 @@ require 'lean_tag/tagging'
6
6
 
7
7
  module LeanTag
8
8
  class Engine < ::Rails::Engine
9
+
9
10
  isolate_namespace LeanTag
11
+
10
12
  end
11
13
  end
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(/[^0-9a-zA-Z]+/, "")
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
@@ -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!(tag, tag_relation) }
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.find_by_name(tag)
59
+ record = Tag.find_or_create_by(name: tag)
51
60
  end
52
61
 
53
- if record.nil?
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 = Tag.where(id: self._taggings(filter).select(:tag_id).distinct)
101
- tags.reject { |t| t.name.in?(tag_names) }.each { |t| self._remove_tag(t, filter) }
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
@@ -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
@@ -1,3 +1,5 @@
1
1
  module LeanTag
2
- VERSION = '1.0.1'
2
+
3
+ VERSION = '1.0.5'
4
+
3
5
  end
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
- attr_accessor :delimiter, :force_lowercase, :remove_unused
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.1
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: 2017-11-19 00:00:00.000000000 Z
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.6.14
106
+ rubygems_version: 2.4.4
107
107
  signing_key:
108
108
  specification_version: 4
109
109
  summary: A lightweight method for tagging content