simple_tags 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,70 @@
1
+ = SimpleTags
2
+
3
+ Simple tagging system for ruby on rails.
4
+
5
+ == Instalation
6
+
7
+ # Gemfile
8
+ gem 'simple_tags'
9
+
10
+
11
+ rails g simple_tags:migrations
12
+ rake db:migrate
13
+
14
+
15
+ == Configuration
16
+
17
+ You should add the Taggings module to the models you want to tag:
18
+
19
+ class SomeTaggableModel < ActiveRecord::Base
20
+ include SimpleTags::Taggable
21
+ end
22
+
23
+ That's it, no configuration needed. Include `SimpleTags::Taggable` to every model you want to tag.
24
+
25
+ == Use
26
+
27
+ === Adding tags
28
+
29
+ model = SomeTaggableModel.first
30
+ model.tags << [SimpleTags::Tag.create(name: 'cool'), SimpleTags::Tag.create(name: 'nice')
31
+
32
+
33
+
34
+ model = SomeTaggableModel.first
35
+ model.tag_list = "cool, nice"
36
+
37
+
38
+ === Listing Tags
39
+
40
+ model = SomeTaggableModel.first
41
+ model.tags
42
+ # Active relation tag list
43
+
44
+
45
+ model = SomeTaggableModel.first
46
+ model.tag_list
47
+ # String with tag names separated by commas
48
+
49
+
50
+ === Taggable listings
51
+
52
+ Dynamic associations are created everytime you include `SimpleTags::Taggable` to a model. For example:
53
+
54
+ class Article < ActiveRecord::Base
55
+ include SimpleTags::Taggable
56
+ end
57
+
58
+ # after that, the Tag models will have an `articles` relation
59
+
60
+ tag = SimpleTags::Tag.first
61
+ tag.articles
62
+
63
+
64
+ You can list all taggings of a tag using the `taggings` relation:
65
+
66
+ tag.taggings
67
+
68
+ = License
69
+
70
+ MIT License. Copyright 2012 Tiago Scolari
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'SimpleTags'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
@@ -0,0 +1,9 @@
1
+ module SimpleTags
2
+ class Tag < ActiveRecord::Base
3
+ self.table_name = :simple_tags_tags
4
+ has_many :taggings, class_name: 'SimpleTags::Tagging', dependent: :destroy
5
+ validates :name, uniqueness: true, presence: true
6
+
7
+ attr_accessible :name
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ module SimpleTags
2
+ class Tagging < ActiveRecord::Base
3
+ self.table_name = :simple_tags_taggings
4
+ belongs_to :tag, class_name: 'SimpleTags::Tag', counter_cache: true
5
+ belongs_to :taggable, polymorphic: true
6
+
7
+ validates :tag_id, uniqueness: { scope: [:taggable_type, :taggable_id] }
8
+ validates :tag, :taggable, presence: true
9
+ end
10
+ end
@@ -0,0 +1,2 @@
1
+ module SimpleTags
2
+ end
@@ -0,0 +1,17 @@
1
+ class CreateTaggings < ActiveRecord::Migration
2
+ def change
3
+ create_table :simple_tags_taggings do |t|
4
+ t.references :tag, null: false
5
+ t.references :taggable, null: false, polymorphic: true
6
+ end
7
+
8
+ add_index :simple_tags_taggings,
9
+ [:tag_id, :taggable_type, :taggable_id],
10
+ name: 'taggings_unq_idx',
11
+ unique: true
12
+
13
+ add_index :simple_tags_taggings,
14
+ [:taggable_type, :taggable_id],
15
+ name: 'taggings_taggable_idx'
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ class CreateTags < ActiveRecord::Migration
2
+ def change
3
+ create_table :simple_tags_tags do |t|
4
+ t.string :name, null: false
5
+ t.integer :taggings_count, default: 0, null: false
6
+ end
7
+ add_index :simple_tags_tags, :name, unique: true
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ rails generate migrations Thing
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,21 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ class SimpleTags::MigrationsGenerator < Rails::Generators::Base
5
+ include Rails::Generators::Migration
6
+ source_root File.expand_path('../../../../../db/migrate', __FILE__)
7
+
8
+ def copy_migrations
9
+ migration_template "create_tags.rb", "db/migrate/create_tags.rb"
10
+ migration_template "create_taggings.rb", "db/migrate/create_taggings.rb"
11
+ end
12
+
13
+ def self.next_migration_number( dirname )
14
+ next_migration_number = current_migration_number(dirname) + 1
15
+ if ActiveRecord::Base.timestamped_migrations
16
+ [Time.now.utc.strftime("%Y%m%d%H%M%S%6N"), "%.20d" % next_migration_number].max
17
+ else
18
+ "%.3d" % next_migration_number
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ module SimpleTags
2
+ class Engine < ::Rails::Engine
3
+
4
+ end
5
+ end
@@ -0,0 +1,43 @@
1
+ module SimpleTags
2
+ module Taggable
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ relation_name = self.name.underscore
7
+ relation_class_name = self.name
8
+
9
+ # Adds a relation in the Tag model for listing this type of taggables
10
+ #
11
+ SimpleTags::Tag.class_eval do
12
+ has_many "#{relation_name}_taggings".to_sym, class_name: 'SimpleTags::Tagging', conditions: ["taggable_type = ?", relation_class_name], foreign_key: 'tag_id'
13
+ has_many relation_name.pluralize.to_sym, class_name: relation_class_name, through: "#{relation_name}_taggings".to_sym, source: "#{relation_name}_filter".to_sym
14
+ end
15
+
16
+ # Adds a relation in the Tagging model for listing this type of taggables
17
+ # This relation exists only to serve the Tag model relation for this object
18
+ #
19
+ SimpleTags::Tagging.class_eval do
20
+ belongs_to "#{relation_name}_filter".to_sym, class_name: relation_class_name, foreign_key: 'taggable_id'
21
+ end
22
+
23
+ has_many :taggings, as: 'taggable', dependent: :destroy, class_name: 'SimpleTags::Tagging'
24
+ has_many :tags, through: :taggings, class_name: 'SimpleTags::Tag'
25
+ end
26
+
27
+ # Public: Return a comma-separated list of this model tags name
28
+ #
29
+ def tag_list
30
+ tags.map(&:name).join(", ")
31
+ end
32
+
33
+ # Public: Sets the model tags
34
+ # `names` must be a list of tag names, separated by commas
35
+ #
36
+ def tag_list=(names)
37
+ self.tags = names.split(",").map do |n|
38
+ SimpleTags::Tag.where(name: n.strip).first_or_create!
39
+ end
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleTags
2
+ VERSION = "0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ require 'simple_tags/engine'
2
+ require 'simple_tags/taggable'
3
+ module SimpleTags
4
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :simple_tags do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_tags
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tiago Scolari
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.8
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.8
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec-rails
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: database_cleaner
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: factory_girl_rails
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: ffaker
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: shoulda-matchers
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: Simple tagging system for Ruby on Rails
111
+ email:
112
+ - tscolari@gmail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - app/models/simple_tags/tag.rb
118
+ - app/models/simple_tags/tagging.rb
119
+ - app/models/simple_tags.rb
120
+ - db/migrate/create_taggings.rb
121
+ - db/migrate/create_tags.rb
122
+ - lib/generators/simple_tags/migrations/migrations_generator.rb
123
+ - lib/generators/simple_tags/migrations/USAGE
124
+ - lib/simple_tags/engine.rb
125
+ - lib/simple_tags/taggable.rb
126
+ - lib/simple_tags/version.rb
127
+ - lib/simple_tags.rb
128
+ - lib/tasks/simple_tags_tasks.rake
129
+ - MIT-LICENSE
130
+ - Rakefile
131
+ - README.rdoc
132
+ homepage: https://github.com/tscolari/simple_tags
133
+ licenses: []
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ none: false
140
+ requirements:
141
+ - - ! '>='
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ segments:
145
+ - 0
146
+ hash: 3595926858561685661
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ none: false
149
+ requirements:
150
+ - - ! '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ segments:
154
+ - 0
155
+ hash: 3595926858561685661
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 1.8.24
159
+ signing_key:
160
+ specification_version: 3
161
+ summary: Simple Tagging Gem
162
+ test_files: []