has_tags 0.0.2

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d073cf765ae66a310cbe1d9a2e8d26e965479374
4
+ data.tar.gz: 51c2a060063fb1f153796cc86b95f252390adeec
5
+ SHA512:
6
+ metadata.gz: 176ad7c46525c301cc366d8ad8a0132bebfeb4799d152e7263eca976813e600e301c7e6ae2531599ad4aaa5d8de00c708ad1fe6c62c7ef872d2e6a6045291bf5
7
+ data.tar.gz: 704a4b62b72c1ca65da8e73659a0c49c2e1f31ca2e4803edf51b0a5e975e919002213662bc0d2720f03fcf483d4e869e19374cc653e07e4095d6a253ed713653
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in has_tags.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Garrett Martin
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,88 @@
1
+ # HasTags
2
+
3
+ HasTags is a lightweight library for easily adding tags to your ActiveRecord models.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'has_tags'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install has_tags
18
+
19
+ ## Usage
20
+
21
+ First, you must run <code>rails g has_tags:install</code> to install the tag migrations.
22
+
23
+ Once the migrations are installed, run <code>rake db:migrate</code>.
24
+
25
+ Now choose a model to add tags to. Let's use <code>Post</code> as an example. All you have to do is add <code>has_tags</code> to the model class definition:
26
+
27
+ ```ruby
28
+ class Post < ActiveRecord::Base
29
+ has_tags
30
+ end
31
+
32
+ ```
33
+
34
+ Your post class can now have associated tags.
35
+
36
+ To set tags on a <code>Post</code> instance, whitelist the <code>tag_list</code> parameter in your controller:
37
+
38
+ ```ruby
39
+ class PostController < ActiveRecord::Base
40
+ # ...
41
+
42
+ private
43
+
44
+ def post_params
45
+ params.require(:post).permit(:param1, :param2, :tag_list)
46
+ end
47
+ end
48
+ ```
49
+
50
+ <code>tag_list</code> should be a string with tags separated by commas, or colons to make a context tag.
51
+
52
+ ### Examples
53
+
54
+ For creating top-level tags, a user can type in tags only separated by commas:
55
+
56
+ "Sports, Food"
57
+
58
+ For creating tags within tags, where to top-level tag acts as the context or parent for the child tag, separate the tags by colons:
59
+
60
+ "Sports:Hockey, Food"
61
+
62
+ This creates three tags: Sports, Hockey and Food. Sports is now the context in which Hockey lives. Now we can call:
63
+
64
+ ```ruby
65
+ context = Tag.find_by(name: "Sports")
66
+
67
+ context.tags # => Hockey tag
68
+ ```
69
+
70
+ The context tag syntax can be nested like so:
71
+
72
+ "Sports:Hockey:Strategy"
73
+
74
+ ```ruby
75
+ strategy = Tag.find_by(name: "Strategy")
76
+
77
+ hockey = strategy.context # => Hockey tag
78
+
79
+ hockey.context # => Sports tag
80
+ ```
81
+
82
+ ## Contributing
83
+
84
+ 1. Fork it
85
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
86
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
87
+ 4. Push to the branch (`git push origin my-new-feature`)
88
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,13 @@
1
+ class TaggableMigration < ActiveRecord::Migration
2
+ def change
3
+ create_table :tags do |t|
4
+ t.string :name
5
+ t.integer :context_id
6
+ end
7
+
8
+ create_table :taggings do |t|
9
+ t.references :tag
10
+ t.references :taggable, polymorphic: true
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'has_tags/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "has_tags"
8
+ spec.version = HasTags::VERSION
9
+ spec.authors = ["Garrett Martin"]
10
+ spec.email = ["me@garrettqmartin.com"]
11
+ spec.description = %q{Simple tagging with contexts.}
12
+ spec.summary = %q{Simple tagging with contexts.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency 'activerecord'
22
+
23
+ spec.add_development_dependency 'sqlite3'
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "database_cleaner"
28
+ spec.add_development_dependency "shoulda-matchers"
29
+ end
@@ -0,0 +1,17 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ module HasTags
4
+ class InstallGenerator < Rails::Generators::Base
5
+ include Rails::Generators::Migration
6
+
7
+ source_root File.expand_path("../../../../db/migrate", __FILE__)
8
+
9
+ def create_migrations
10
+ migration_template "taggable_migration.rb", "db/migrate/taggable_migration.rb"
11
+ end
12
+
13
+ def self.next_migration_number(path)
14
+ @migration_number = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i.to_s
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,10 @@
1
+ require "has_tags/version"
2
+ require "active_record"
3
+
4
+ module HasTags
5
+ require "has_tags/tag"
6
+ require "has_tags/tagging"
7
+ require "has_tags/taggable"
8
+
9
+ ActiveRecord::Base.extend Taggable
10
+ end
@@ -0,0 +1,10 @@
1
+ module HasTags
2
+ class Tag < ::ActiveRecord::Base
3
+ belongs_to :context, class_name: "Tag"
4
+
5
+ has_many :tags, foreign_key: :context_id
6
+ has_many :taggings
7
+
8
+ validates :name, uniqueness: true, presence: true
9
+ end
10
+ end
@@ -0,0 +1,46 @@
1
+ module HasTags
2
+ module Taggable
3
+ # This implementation requires that the taggable module be
4
+ # used like this:
5
+ #
6
+ # include HasTags::Taggable
7
+ #
8
+ # in whatever model you wish to have tags.
9
+ #
10
+ #
11
+ # def self.included(base)
12
+ # base.class_eval do
13
+ # has_many :taggings, as: :taggable, class_name: "HasTags::Tagging"
14
+ # has_many :tags, through: :taggings, class_name: "HasTags::Tag"
15
+ # end
16
+ # end
17
+
18
+ def has_tags
19
+ has_many :taggings, as: :taggable, class_name: "HasTags::Tagging"
20
+ has_many :tags, through: :taggings, class_name: "HasTags::Tag"
21
+
22
+ include InstanceMethods
23
+ end
24
+
25
+ module InstanceMethods
26
+ def tag_list
27
+ tags.map(&:name).join(", ")
28
+ end
29
+
30
+ def tag_list=(tag_names)
31
+ tag_names.split(",").map do |tag_name|
32
+ names = tag_name.strip.split(":")
33
+ names.each_with_index do |name, index|
34
+ if index < 1
35
+ tag = HasTags::Tag.where(name: name).first_or_create!
36
+ else
37
+ tag = HasTags::Tag.where(name: name, context_id: Tag.find_by(name: names[index-1]).id).first_or_create!
38
+ end
39
+ HasTags::Tagging.where(tag_id: tag.id, taggable: self).first_or_create!
40
+ end
41
+ end
42
+ end
43
+
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,8 @@
1
+ module HasTags
2
+ class Tagging < ::ActiveRecord::Base
3
+ validates :tag_id, presence: true
4
+
5
+ belongs_to :tag
6
+ belongs_to :taggable, polymorphic: true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module HasTags
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,28 @@
1
+ require 'has_tags'
2
+ require 'support/database_cleaner'
3
+ require 'support/taggable_model'
4
+ require 'database_cleaner'
5
+ require 'shoulda/matchers'
6
+
7
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3",
8
+ :database => ":memory:")
9
+
10
+ ActiveRecord::Schema.define do
11
+ self.verbose = false
12
+
13
+ create_table :tags, force: true do |t|
14
+ t.string :name
15
+ t.integer :context_id
16
+ end
17
+
18
+ create_table :taggings, force: true do |t|
19
+ t.references :tag
20
+ t.references :taggable, polymorphic: true
21
+ end
22
+
23
+ create_table :taggable_models, force: true do |t|
24
+ t.string :name
25
+ end
26
+
27
+ end
28
+
@@ -0,0 +1,23 @@
1
+ RSpec.configure do |config|
2
+
3
+ config.before(:suite) do
4
+ DatabaseCleaner.clean_with(:truncation)
5
+ end
6
+
7
+ config.before(:each) do
8
+ DatabaseCleaner.strategy = :transaction
9
+ end
10
+
11
+ config.before(:each, :js => true) do
12
+ DatabaseCleaner.strategy = :truncation
13
+ end
14
+
15
+ config.before(:each) do
16
+ DatabaseCleaner.start
17
+ end
18
+
19
+ config.after(:each) do
20
+ DatabaseCleaner.clean
21
+ end
22
+
23
+ end
@@ -0,0 +1,3 @@
1
+ class TaggableModel < ActiveRecord::Base
2
+ has_tags
3
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe HasTags::Tag do
4
+ it "should create tag with valid name" do
5
+ HasTags::Tag.create(name: "Sports")
6
+ expect(HasTags::Tag.all.count).to eq(1)
7
+ end
8
+
9
+ it "should fail without a name" do
10
+ HasTags::Tag.create(name: nil)
11
+ expect(HasTags::Tag.all.count).to eq(0)
12
+ end
13
+
14
+ it "should fail if a tag with that name already exists" do
15
+ HasTags::Tag.create(name: "Sports")
16
+ HasTags::Tag.create(name: "Sports")
17
+ expect(HasTags::Tag.all.count).to eq(1)
18
+ end
19
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe TaggableModel do
4
+ let (:model) { TaggableModel.create(name: "post") }
5
+
6
+ it { should have_many(:taggings) }
7
+ it { should have_many(:tags) }
8
+
9
+ context "setting tag list" do
10
+ it "should be able to use two separate tags" do
11
+ tag_names = "Sports, Food"
12
+ model.tag_list = tag_names
13
+
14
+ expect(model.tags.count).to eq(2)
15
+ end
16
+
17
+ it "should create a context tag with colon" do
18
+ tag_names = "Sports:Lacrosse"
19
+ model.tag_list = tag_names
20
+
21
+ expect(HasTags::Tag.last.context.name).to eq("Sports")
22
+ end
23
+
24
+ it "should support multiple nested contexts" do
25
+ tag_names = "Sports:Lacrosse:Plays"
26
+ model.tag_list = tag_names
27
+
28
+ expect(HasTags::Tag.last.context.name).to eq("Lacrosse")
29
+ end
30
+ end
31
+
32
+ it "should retrieve tag list" do
33
+ tag_names = "Sports:Lacrosse"
34
+ model.tag_list = tag_names
35
+
36
+ expect(model.tag_list).to eq("Sports, Lacrosse")
37
+ end
38
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe HasTags::Tagging do
4
+ let(:tag) { HasTags::Tag.create(name: "Sports") }
5
+ let(:model) { TaggableModel.create(name: "Post") }
6
+
7
+ it "should create a tagging" do
8
+ HasTags::Tagging.create(tag_id: tag.id, taggable_id: model.id, taggable_type: model.class.to_s)
9
+ expect(HasTags::Tagging.count).to eq(1)
10
+ end
11
+
12
+ it "should fail without tag_id" do
13
+ HasTags::Tagging.create(taggable_id: model.id, taggable_type: model.class.to_s)
14
+ expect(HasTags::Tagging.count).to eq(0)
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,168 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_tags
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Garrett Martin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: database_cleaner
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: shoulda-matchers
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Simple tagging with contexts.
112
+ email:
113
+ - me@garrettqmartin.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - .gitignore
119
+ - Gemfile
120
+ - LICENSE.txt
121
+ - README.md
122
+ - Rakefile
123
+ - db/migrate/taggable_migration.rb
124
+ - has_tags.gemspec
125
+ - lib/generators/has_tags/install_generator.rb
126
+ - lib/has_tags.rb
127
+ - lib/has_tags/tag.rb
128
+ - lib/has_tags/taggable.rb
129
+ - lib/has_tags/tagging.rb
130
+ - lib/has_tags/version.rb
131
+ - spec/spec_helper.rb
132
+ - spec/support/database_cleaner.rb
133
+ - spec/support/taggable_model.rb
134
+ - spec/tag_spec.rb
135
+ - spec/taggable_spec.rb
136
+ - spec/tagging_spec.rb
137
+ homepage: ''
138
+ licenses:
139
+ - MIT
140
+ metadata: {}
141
+ post_install_message:
142
+ rdoc_options: []
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - '>='
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubyforge_project:
157
+ rubygems_version: 2.3.0
158
+ signing_key:
159
+ specification_version: 4
160
+ summary: Simple tagging with contexts.
161
+ test_files:
162
+ - spec/spec_helper.rb
163
+ - spec/support/database_cleaner.rb
164
+ - spec/support/taggable_model.rb
165
+ - spec/tag_spec.rb
166
+ - spec/taggable_spec.rb
167
+ - spec/tagging_spec.rb
168
+ has_rdoc: