easy_tags 0.1.0.pre.alpha

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 946ce5a345e151980fa49a96c8595025e20398f0
4
+ data.tar.gz: 53b9c21505e59edaee95dd3937b8164b8b4f7f97
5
+ SHA512:
6
+ metadata.gz: 37abe520d5e1fafb8974be26a1ab02175a2f19f26eab84eee1673b848d72abbaa10bfbdb87024454aac16ab3cbed9a55e5022fc6b9304664c392dee6234b0da3
7
+ data.tar.gz: aa63f981b6220bb317dbad45b349a417da549d7c63f8c8c40586452d49b9a38132cd91e1ba5dae0ccd07572b7e0d81bddc5bc4ff14ecd5d8c41bcb39a8611ef0
data/.codeclimate.yml ADDED
@@ -0,0 +1,3 @@
1
+ plugins:
2
+ rubocop:
3
+ enabled: true
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .idea
13
+ *.lock
14
+ *.gem
15
+ gemfiles/.bundle/
16
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,15 @@
1
+ Metrics/LineLength:
2
+ Max: 120
3
+
4
+ Style/StringLiterals:
5
+ EnforcedStyle: single_quotes
6
+
7
+ Style/SymbolArray:
8
+ Enabled: true
9
+
10
+ Layout/IndentationConsistency:
11
+ EnforcedStyle: rails
12
+
13
+ AllCops:
14
+ Exclude:
15
+ - 'spec/**/*'
data/.travis.yml ADDED
@@ -0,0 +1,33 @@
1
+ sudo: false
2
+ language: ruby
3
+ cache: bundler
4
+ before_script: bundle update
5
+ before_install:
6
+ - gem uninstall -v '>= 2' -i $(rvm gemdir)@global -ax bundler || true
7
+ - gem install bundler -v '< 2'
8
+ before_script:
9
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
10
+ - chmod +x ./cc-test-reporter
11
+ - ./cc-test-reporter before-build
12
+ script:
13
+ - bundle exec rspec
14
+ after_script:
15
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
16
+ rvm:
17
+ - 2.3.8
18
+ - 2.4.5
19
+ - 2.5.3
20
+ - 2.6.0
21
+ - ruby-head
22
+ gemfile:
23
+ - Gemfile
24
+ - gemfiles/activerecord_5.0.gemfile
25
+ - gemfiles/activerecord_5.1.gemfile
26
+ - gemfiles/activerecord_5.2.gemfile
27
+ matrix:
28
+ allow_failures:
29
+ - rvm: ruby-head
30
+ fast_finish: true
31
+ env:
32
+ global:
33
+ - CC_TEST_REPORTER_ID=c7f705db7f3e2658ca529a196cd65b724f6b674a7ed6ac2e01928135babdac35
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --no-private
data/Appraisals ADDED
@@ -0,0 +1,14 @@
1
+ appraise 'activerecord-5.0' do
2
+ gem 'activerecord', '~> 5.0.0'
3
+ gem 'activesupport', '~> 5.0.0'
4
+ end
5
+
6
+ appraise 'activerecord-5.1' do
7
+ gem 'activerecord', '~> 5.1.0'
8
+ gem 'activesupport', '~> 5.1.0'
9
+ end
10
+
11
+ appraise 'activerecord-5.2' do
12
+ gem 'activerecord', '~> 5.2.0'
13
+ gem 'activesupport', '~> 5.2.0'
14
+ end
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in easy_tags.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Iwo Dziechciarow
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ <!-- START doctoc generated TOC please keep comment here to allow auto update -->
2
+ <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
3
+ **Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*
4
+
5
+ - [Migrating from ActsAsTaggableOn to EasyTags](#migrating-from-actsastaggableon-to-easytags)
6
+ - [Database](#database)
7
+ - [Features](#features)
8
+
9
+ <!-- END doctoc generated TOC please keep comment here to allow auto update -->
10
+
11
+ ## Migrating from ActsAsTaggableOn to EasyTags
12
+
13
+ ### Database
14
+ `EasyTags` uses the same table structure as `ActsAsTaggableOn` - you most probably do not have to do any migrations
15
+
16
+ ### Features
17
+ in comparison to `ActsAsTaggableOn` some features/behaviors were deliberately not included in `EasyTags`:
18
+ - singularization/pluralizion of context names (we use flat naming)
19
+ - `Tag Ownership`
20
+ - Query helpers (ie. `tagged_with`)
21
+ - statistic helpers (ie. `most_used`, `least_used`)
22
+ - `Dynamic Tag Contexts`
23
+
24
+ ### Configuration
25
+ `EasyTags` does not implement configuration options like:
26
+ `delimiter`, `force_lowercase`, `force_parameterize` etc.
27
+
28
+ you can use custom parser for that
29
+
30
+ ### Auto-generated context methods
31
+ `EasyTags` uses consistent types and naming
32
+
33
+ | | `easy_tags` | `acts_as_tagabble_on` |
34
+ |------------------------------------|----------------------|-----------------------|
35
+ | `has_many` to `Tag` relation | context_tags | context(pluralized) |
36
+ | `has_many` to `Taggings` relation | context_taggings | context(singularized)_taggings |
37
+ | set tags using string of tag names | context_list=(value) | context(singularized)_list=(value) |
38
+ | get string of tag names | context_list | - |
39
+ | set tags with array of tag names | context=(value) | context(singularized)_list=(value) |
40
+ | get array of tag names | context | context(singularized)_list |
41
+
data/README.md ADDED
@@ -0,0 +1,344 @@
1
+ [![Maintainability](https://api.codeclimate.com/v1/badges/5c35fe854f8cbd3f1209/maintainability)](https://codeclimate.com/github/iiwo/easy_tags/maintainability)
2
+ [![Build Status](https://travis-ci.org/iiwo/easy_tags.svg?branch=master)](https://travis-ci.org/iiwo/easy_tags)
3
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/5c35fe854f8cbd3f1209/test_coverage)](https://codeclimate.com/github/iiwo/easy_tags/test_coverage)
4
+
5
+ <!-- START doctoc generated TOC please keep comment here to allow auto update -->
6
+ <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
7
+ **Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*
8
+
9
+ - [EasyTags](#easytags)
10
+ - [Migrating from ActsAsTaggableOn](#migrating-from-actsastaggableon)
11
+ - [Installation](#installation)
12
+ - [Post Installation](#post-installation)
13
+ - [Usage](#usage)
14
+ - [Setup](#setup)
15
+ - [Interface](#interface)
16
+ - [Examples](#examples)
17
+ - [Adding/Removing tags](#addingremoving-tags)
18
+ - [Querying](#querying)
19
+ - [Context relations](#context-relations)
20
+ - [Global relations](#global-relations)
21
+ - [Callbacks](#callbacks)
22
+ - [Instance scope callbacks](#instance-scope-callbacks)
23
+ - [Active Support Instrumentation custom notifications](#active-support-instrumentation-custom-notifications)
24
+ - [Tag notifications](#tag-notifications)
25
+ - [Tagging notifications](#tagging-notifications)
26
+ - [Dirty objects](#dirty-objects)
27
+ - [Configuration](#configuration)
28
+ - [Testing](#testing)
29
+ - [Development](#development)
30
+ - [Contributing](#contributing)
31
+ - [License](#license)
32
+
33
+ <!-- END doctoc generated TOC please keep comment here to allow auto update -->
34
+
35
+ # EasyTags
36
+
37
+ Easy contextual tagging for Rails
38
+
39
+ ## Migrating from ActsAsTaggableOn
40
+
41
+ This gem was inspired by [ActsAsTaggableOn](https://github.com/mbleigh/acts-as-taggable-on).
42
+ It was built on similar model, but rewritten from scratch and simplified with better maintainability in mind.
43
+ Considerable amount of features has been removed and we've made some breaking changes.
44
+ Please refer to [ActsAsTaggableOn migration notes](https://github.com/iiwo/easy_tags/blob/master/MIGRATING_FROM_AATO.MD) if you want to migrate
45
+
46
+ ## Installation
47
+
48
+ Add this line to your application's Gemfile:
49
+
50
+ ```ruby
51
+ gem 'easy_tags'
52
+ ```
53
+
54
+ And then execute:
55
+ ```shell
56
+ $ bundle
57
+ ```
58
+
59
+ ### Post Installation
60
+
61
+ generate migration with:
62
+ ```shell
63
+ rails g easy_tags:migration
64
+ ```
65
+
66
+ ## Usage
67
+
68
+ ### Setup
69
+
70
+ in your ActiveRecord model add:
71
+
72
+ ```ruby
73
+ include EasyTags::Taggable
74
+
75
+ easy_tags_on :highlights
76
+ ```
77
+
78
+ with multiple tag contexts:
79
+ ```ruby
80
+ include EasyTags::Taggable
81
+
82
+ easy_tags_on :highlights, :tags, :notes
83
+ ```
84
+
85
+ ### Interface
86
+ upon the `easy_tags_on` declaration:
87
+ ```ruby
88
+ easy_tags_on :highlights
89
+ ```
90
+
91
+ the following methods will be auto-generated and made available for your model instance (`highlights` being an example context name):
92
+
93
+ | description | method |
94
+ |------------------------------------|---------------------------|
95
+ | set tags using string of tag names | `highlights_list=(value)` |
96
+ | get string of tag names | `highlights_list` |
97
+ | set tags with array of tag names | `highlights=(value)` |
98
+ | get array of tag names | `highlights` |
99
+
100
+ - the String accessor is helpful when working with client side tagging UI solutions such as ie. [select2](https://select2.org/tagging)
101
+ - the Array accessor gives you convenient array item manipulation
102
+
103
+ ### Examples
104
+
105
+ #### Adding/Removing tags
106
+
107
+ Using Array accessor
108
+ ```ruby
109
+ # assign/replace tags (comma separated using the default parser)
110
+ model.highlights = ['My Tag', 'Second Tag', 'Some Other tag']
111
+ # => [
112
+ # [0] "My Tag",
113
+ # [1] "Second Tag",
114
+ # [2] "Some Other tag"
115
+ # ]
116
+
117
+ # add tags
118
+ model.highlights.add('One More', 'And Another')
119
+ model.highlights
120
+ # => [
121
+ # [0] "My Tag",
122
+ # [1] "Second Tag",
123
+ # [2] "Some Other tag",
124
+ # [3] "One More",
125
+ # [4] "And Another"
126
+ # ]
127
+
128
+ # remove a single tag
129
+ model.highlights.remove('One More')
130
+ model.highlights
131
+ # => [
132
+ # [0] "My Tag",
133
+ # [1] "Second Tag",
134
+ # [2] "Some Other tag",
135
+ # [3] "And Another"
136
+ # ]
137
+
138
+ # remember to persist changes
139
+ model.save
140
+ ```
141
+
142
+ Using String accessor
143
+ ```ruby
144
+ # add multiple tags (comma separated using the default parser)
145
+ model.highlights_list = 'My Tag, Second Tag, Some Other tag'
146
+ # > "My Tag, Second Tag, Some Other tag"
147
+
148
+ # remove a single tag
149
+ model.highlights_list = 'My Tag, Second Tag'
150
+ # > "My Tag, Second Tag"
151
+
152
+ # remove all tags
153
+ model.highlights_list = ''
154
+
155
+ # remember to persist changes
156
+ model.save
157
+ ```
158
+
159
+ #### Querying
160
+ `EasyTags` does not offer any query helper scopes, but it's fairly easy to query without them.
161
+
162
+ ##### Context relations
163
+ Upon the `easy_tags_on` declaration:
164
+ ```ruby
165
+ easy_tags_on :highlights
166
+ ```
167
+
168
+ the following relations will be auto-generated and made available for your model instance (`highlights` being an example context name):
169
+
170
+ | description | relation |
171
+ |----------------------------------------------|-------------------------|
172
+ | `has_many` to `EasyTags::Tag` relation | highlights_tags |
173
+ | `has_many` to `EasyTags::Taggings` relation | highlights_taggings |
174
+
175
+ - `EasyTags::Tag` model represents a single tag name
176
+ - `EasyTags::Taggings` represents a join table between your model and the `EasyTags::Tag` model, it also holds the tag context value
177
+
178
+ Find all model instances with a specific tag name:
179
+ ```ruby
180
+ MyModel.joins(:highlights_tags).where(tags: { name: 'My Tag' })
181
+ ```
182
+
183
+ Eager loading:
184
+ ```ruby
185
+ MyModel.includes(:highlights_tags)
186
+ ```
187
+
188
+ ##### Global relations
189
+ Upon the `EasyTags::Taggable` inclusion
190
+ ```ruby
191
+ include EasyTags::Taggable
192
+ ```
193
+ the fallowing context independent relations will be auto-generated and made available for your model instance:
194
+
195
+ | description | relation |
196
+ |----------------------------------------------|-----------------------------------------|
197
+ | `has_many` to `EasyTags::Taggings` relation | has_many :taggings, as: :taggable |
198
+ | `has_many` to `EasyTags::Tag` relation | has_many :base_tags, through: :taggings |
199
+
200
+ You can use them for querying multiple contexts
201
+ ```ruby
202
+ MyModel.joins(:base_tags).where(taggings: { context: %w[highlights billing_highlights] })
203
+ ```
204
+
205
+ ### Callbacks
206
+
207
+ #### Instance scope callbacks
208
+
209
+ ```ruby
210
+ include EasyTags::Taggable
211
+
212
+ easy_tags_on(
213
+ highlights: { after_add: :add_tag_callback, after_remove: -> (tagging) { puts "removed #{tagging.tag.name}" } }
214
+ )
215
+
216
+ def add_tag_callback(tagging)
217
+ puts "added #{tagging.tag.name}"
218
+ end
219
+ ```
220
+
221
+ #### Active Support Instrumentation custom notifications
222
+
223
+ ##### Tag notifications
224
+
225
+ ```ruby
226
+ ActiveSupport::Notifications.subscribe 'easy_tag.tag_added' do |tag|
227
+ puts "added #{tag.name}"
228
+ end
229
+ ```
230
+
231
+ ```ruby
232
+ ActiveSupport::Notifications.subscribe 'easy_tag.tag_removed' do |tag|
233
+ puts "removed #{tag.name}"
234
+ end
235
+ ```
236
+
237
+ ##### Tagging notifications
238
+
239
+ ```ruby
240
+ ActiveSupport::Notifications.subscribe 'easy_tag.tagging_added.YOUR_MODEL_TABLEIZED.CONTEXT_NAME' do |tagging|
241
+ puts "added #{tagging.tag.name}"
242
+ end
243
+ ```
244
+
245
+ ```ruby
246
+ ActiveSupport::Notifications.subscribe 'easy_tag.tagging_added.YOUR_MODEL_TABLEIZED.CONTEXT_NAME' do |tagging|
247
+ puts "removed #{tagging.tag.name}"
248
+ end
249
+ ```
250
+
251
+ ### Dirty objects
252
+
253
+ `EasyTags` implements `ActiveModel::Dirty` attribute changes tracking fot all the context String accessors (the `CONTEXT_NAME_list` attributes)
254
+
255
+ ```ruby
256
+ model.highlights
257
+ # []
258
+
259
+ model.highlights = ['Tag One', 'Tag Two']
260
+ # [
261
+ # [0] "Tag One",
262
+ # [1] "Tag Two"
263
+ # ]
264
+
265
+ model.changed?
266
+ # true
267
+
268
+ model.highlights_list_changed?
269
+ # true
270
+
271
+ model.highlights_list_was
272
+ # ""
273
+
274
+ model.highlights_list_change
275
+ # [
276
+ # [0] "",
277
+ # [1] "Tag One,Tag Two"
278
+ # ]
279
+
280
+ model.save
281
+ # true
282
+
283
+ model.changed?
284
+ # false
285
+
286
+ model.highlights_list_changed?
287
+ # false
288
+
289
+ model.previous_changes
290
+ # {
291
+ # "highlights_list" => [
292
+ # [0] nil,
293
+ # [1] "Tag One,Tag Two"
294
+ # ]
295
+ # }
296
+
297
+ model.highlights_list_previously_changed?
298
+ # true
299
+
300
+ model.highlights_list_previous_change
301
+ # [
302
+ # [0] nil,
303
+ # [1] "Tag One,Tag Two"
304
+ # ]
305
+ ```
306
+
307
+ ## Configuration
308
+
309
+ ```ruby
310
+ # config/initializers/easy_tags.rb
311
+
312
+ EasyTags.setup do |config|
313
+ config.tags_table = :tags
314
+ config.taggings_table = :taggings
315
+ config.parser = EasyTags::Parsers::Default
316
+ config.generator = EasyTags::Generators::Default
317
+ end
318
+ ```
319
+
320
+ You can customize db table names with `tags_table` and `taggings_table` options.
321
+
322
+ You can customize the parser and the generator to use different separators, filtering or processing.
323
+ The default parser uses comma as separator and is case sensitive.
324
+
325
+
326
+ ## Testing
327
+ ```
328
+ bundle exec appraisal install
329
+ bundle exec appraisal rspec
330
+ ```
331
+
332
+ ## Development
333
+
334
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
335
+
336
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
337
+
338
+ ## Contributing
339
+
340
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/easy_tags. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
341
+
342
+ ## License
343
+
344
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'easy_tags'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require 'pry'
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/easy_tags.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'easy_tags/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'easy_tags'
7
+ spec.version = EasyTags::VERSION
8
+ spec.authors = ['Iwo Dziechciarow']
9
+ spec.email = ['iiwo@o2.pl']
10
+
11
+ spec.summary = 'EasyTags allows you to tag a single model on several contexts'
12
+ spec.description = 'Easy tagging for Rails'
13
+ spec.homepage = 'https://github.com/iiwo/easy_tags'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_runtime_dependency 'activerecord', '~> 5.0'
24
+ spec.add_runtime_dependency 'activesupport', '~> 5.0'
25
+
26
+ spec.add_development_dependency 'appraisal'
27
+ spec.add_development_dependency 'database_cleaner'
28
+ spec.add_development_dependency 'rspec'
29
+ spec.add_development_dependency 'rspec-rails'
30
+ spec.add_development_dependency 'simplecov'
31
+ spec.add_development_dependency 'simplecov-console'
32
+ spec.add_development_dependency 'sqlite3', '~> 1.3.6'
33
+ end
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gem 'activerecord', '~> 5.0.0'
6
+ gem 'activesupport', '~> 5.0.0'
7
+
8
+ gemspec path: '../'
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gem 'activerecord', '~> 5.1.0'
6
+ gem 'activesupport', '~> 5.1.0'
7
+
8
+ gemspec path: '../'
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gem 'activerecord', '~> 5.2.0'
6
+ gem 'activesupport', '~> 5.2.0'
7
+
8
+ gemspec path: '../'
@@ -0,0 +1,24 @@
1
+ module EasyTags
2
+ module Generators
3
+ # Default generator for [Array] -> [String] conversion
4
+ class Default
5
+ class << self
6
+ # Generates a new String using the given Array
7
+ #
8
+ # @param [Array] tag_list
9
+ # @return [String]
10
+ #
11
+ # Example:
12
+ # EasyTags::Generators::Default.generate(['One', 'Two', 'Three'])
13
+ # 'One, Two, Three'
14
+ def generate(tag_list)
15
+ tag_list ||= []
16
+
17
+ return '' if tag_list.empty?
18
+
19
+ tag_list.join(',')
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,34 @@
1
+ module EasyTags
2
+ module Options
3
+ # Represents a single callback item
4
+ class Callback
5
+ attr_reader :context
6
+
7
+ TYPES = %i[after_add after_remove].freeze
8
+
9
+ # @param [Proc] callback
10
+ # @param [Symbol] type
11
+ def initialize(callback:, type:)
12
+ @callback = callback
13
+ @type = type
14
+
15
+ raise "unknown callback type - must be one of #{TYPES}" unless TYPES.include?(type)
16
+ end
17
+
18
+ # @return [String]
19
+ attr_reader :type
20
+
21
+ # invoke callback for a taggable object for a specific tagging
22
+ #
23
+ # @param [Object] taggable
24
+ # @param [Object] tagging
25
+ def run(taggable:, tagging:)
26
+ if @callback.is_a?(Symbol)
27
+ taggable.send(@callback, tagging)
28
+ elsif @callback.is_a?(Proc)
29
+ taggable.instance_exec tagging, &@callback
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end