acts_as_taggable_simple 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.
- data/Gemfile +5 -0
- data/MIT-LICENSE +20 -0
- data/Manifest +21 -0
- data/README.rdoc +118 -0
- data/Rakefile +38 -0
- data/acts_as_taggable_simple.gemspec +29 -0
- data/init.rb +3 -0
- data/lib/active_record/acts/taggable.rb +66 -0
- data/lib/acts_as_taggable_simple.rb +6 -0
- data/lib/generators/acts_as_taggable_simple/migration/migration_generator.rb +35 -0
- data/lib/generators/acts_as_taggable_simple/migration/templates/active_record/migration.rb +20 -0
- data/lib/tag.rb +34 -0
- data/lib/tag_list.rb +5 -0
- data/lib/tagging.rb +8 -0
- data/spec/acts_as_taggable_simple/acts_as_taggable_simple_spec.rb +14 -0
- data/spec/acts_as_taggable_simple/spec_helper.rb +1 -0
- data/spec/acts_as_taggable_simple/taggable_spec.rb +65 -0
- data/spec/database.yml +3 -0
- data/spec/models.rb +6 -0
- data/spec/schema.rb +18 -0
- data/spec/spec_helper.rb +33 -0
- metadata +83 -0
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 [name of plugin creator]
|
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/Manifest
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Gemfile
|
2
|
+
MIT-LICENSE
|
3
|
+
README.rdoc
|
4
|
+
Rakefile
|
5
|
+
acts_as_taggable_simple.gemspec
|
6
|
+
init.rb
|
7
|
+
lib/active_record/acts/taggable.rb
|
8
|
+
lib/acts_as_taggable_simple.rb
|
9
|
+
lib/generators/acts_as_taggable_simple/migration/migration_generator.rb
|
10
|
+
lib/generators/acts_as_taggable_simple/migration/templates/active_record/migration.rb
|
11
|
+
lib/tag.rb
|
12
|
+
lib/tag_list.rb
|
13
|
+
lib/tagging.rb
|
14
|
+
spec/acts_as_taggable_simple/acts_as_taggable_simple_spec.rb
|
15
|
+
spec/acts_as_taggable_simple/spec_helper.rb
|
16
|
+
spec/acts_as_taggable_simple/taggable_spec.rb
|
17
|
+
spec/database.yml
|
18
|
+
spec/models.rb
|
19
|
+
spec/schema.rb
|
20
|
+
spec/spec_helper.rb
|
21
|
+
Manifest
|
data/README.rdoc
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
= ActsAsTaggableSimple
|
2
|
+
|
3
|
+
=== Overview
|
4
|
+
|
5
|
+
ActsAsTaggableSimple provides an +acts_as+ style method to
|
6
|
+
ActiveRecord::Base for taggable records. It also provides
|
7
|
+
the following methods to a taggable model:
|
8
|
+
|
9
|
+
* tag_list - returns an Array of tag String objects
|
10
|
+
* tag_list= - takes either a String of tags or an array of tag String objects and sets the tags for the object
|
11
|
+
|
12
|
+
acts_as_taggable provides two new attributes for a model:
|
13
|
+
|
14
|
+
* tags - the ActsAsTaggable::Tag objects associated with the taggable item
|
15
|
+
* taggings - the ActsAsTaggable::Tagging join objects associated with the taggable item
|
16
|
+
|
17
|
+
|
18
|
+
=== Installation
|
19
|
+
|
20
|
+
For Rails 3 applications you should add the following
|
21
|
+
line to your Gemfile:
|
22
|
+
gem 'acts_as_taggable_simple'
|
23
|
+
Then run:
|
24
|
+
bundle install
|
25
|
+
|
26
|
+
You then need to run the migration generator to generate
|
27
|
+
the necessary migration for acts_as_taggable_simple to
|
28
|
+
work properly.
|
29
|
+
rails generate acts_as_taggable_simple:migration
|
30
|
+
|
31
|
+
Next, run the migration. NOTE: you cannot have a table
|
32
|
+
named +tags+ or *taggings*, as these names are used by
|
33
|
+
acts_as_taggable_simple.
|
34
|
+
rake db:migrate
|
35
|
+
|
36
|
+
That's it, you are ready to start using the gem.
|
37
|
+
|
38
|
+
|
39
|
+
=== Getting Started
|
40
|
+
|
41
|
+
This is a walkthrough for setting up and using a
|
42
|
+
taggable model. We will be creating an application
|
43
|
+
for taking notes that we want to tag for easy searching
|
44
|
+
later on.
|
45
|
+
|
46
|
+
The first step is to make our ActiveRecord model
|
47
|
+
taggable.
|
48
|
+
class Note < ActiveRecord::Base
|
49
|
+
attr_accessible :content
|
50
|
+
acts_as_taggable
|
51
|
+
end
|
52
|
+
|
53
|
+
We can then create a new note in the usual manner.
|
54
|
+
note = Note.new :content => "acts_as_taggable_simple is so simple!"
|
55
|
+
|
56
|
+
We can give the note tags.
|
57
|
+
note.tag_list = "rails tags gem"
|
58
|
+
|
59
|
+
We can also give the note tags by passing an Array of String's.
|
60
|
+
note.tag_list = %w{rails tags gem}
|
61
|
+
|
62
|
+
We can retrieve an Array of tag String's for our note.
|
63
|
+
note.tag_list # returns ["rails", "tags", "gem"]
|
64
|
+
|
65
|
+
Note that our note will not be tagged until we save it.
|
66
|
+
note.save
|
67
|
+
|
68
|
+
Now when we retrieve our note from active record,
|
69
|
+
it will retain the tags we have given it.
|
70
|
+
|
71
|
+
If necessary we can directly manipulate a note's tag objects.
|
72
|
+
This should not be necessary though, and for normal use we can
|
73
|
+
do all of our tag editing through tag_list.
|
74
|
+
note.tags << ActsAsTaggableSimple::Tag.new :name => 'why_did_we_do_this?'
|
75
|
+
|
76
|
+
We can also manipulate the join objects by accessing taggings on a note.
|
77
|
+
Again, there is usually never a need to do this.
|
78
|
+
note.taggings # returns all of the ActsAsTaggableSimple::Tagging objects for our note
|
79
|
+
|
80
|
+
This is all we can do with this gem. Things like searching are left to
|
81
|
+
the user or solutions like:
|
82
|
+
http://github.com/ernie/meta_search
|
83
|
+
|
84
|
+
If you want to do cloud calculations on tags, this task is left to you.
|
85
|
+
|
86
|
+
|
87
|
+
=== Example
|
88
|
+
|
89
|
+
app/models/taggable_model.rb
|
90
|
+
|
91
|
+
class TaggableModel < ActiveRecord::Base
|
92
|
+
acts_as_taggable
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
rails console
|
97
|
+
|
98
|
+
t = TaggableModel.new :tag_list => "rails css javascript"
|
99
|
+
t.save
|
100
|
+
t.tag_list # returns ["rails", "css", "javascript"]
|
101
|
+
t.tag_list = "rails html js"
|
102
|
+
t.save
|
103
|
+
t.tag_list # returns ["rails", "html", "js"]
|
104
|
+
# note that tag_list is updated immediately after setting
|
105
|
+
# it, while tags and taggings are only updated after
|
106
|
+
# saving the model
|
107
|
+
|
108
|
+
|
109
|
+
=== Credits/Thanks
|
110
|
+
|
111
|
+
Many thank you's to the creators of the acts-as-taggable-on gem, which
|
112
|
+
if it was not obvious, this gem borrows quite heavily
|
113
|
+
from. This gem is in fact meant to be a stripped down
|
114
|
+
version of acts-as-taggable-on.
|
115
|
+
|
116
|
+
=== Copyright
|
117
|
+
|
118
|
+
Copyright (c) 2011 Hollin R. Wilkins, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require 'echoe'
|
5
|
+
require 'rubygems'
|
6
|
+
|
7
|
+
desc 'Default: run unit tests.'
|
8
|
+
task :default => :spec
|
9
|
+
|
10
|
+
RSpec::Core::RakeTask.new do |t|
|
11
|
+
pattern = "spec/**/*_spec.rb"
|
12
|
+
end
|
13
|
+
|
14
|
+
desc 'Test the acts_as_taggable_simple plugin.'
|
15
|
+
Rake::TestTask.new(:test) do |t|
|
16
|
+
t.libs << 'lib'
|
17
|
+
t.libs << 'test'
|
18
|
+
t.pattern = 'test/**/*_test.rb'
|
19
|
+
t.verbose = true
|
20
|
+
end
|
21
|
+
|
22
|
+
desc 'Generate documentation for the acts_as_taggable_simple plugin.'
|
23
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
24
|
+
rdoc.rdoc_dir = 'rdoc'
|
25
|
+
rdoc.title = 'ActsAsTaggableSimple'
|
26
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
27
|
+
rdoc.rdoc_files.include('README')
|
28
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
29
|
+
end
|
30
|
+
|
31
|
+
Echoe.new('acts_as_taggable_simple', '0.0.2') do |p|
|
32
|
+
p.description = "Adds acts_as_taggable to ActiveRecord::Model to easily tag objects, no extra fluff"
|
33
|
+
p.url = "http://github.com/chromaticbum/acts_as_taggable_simple"
|
34
|
+
p.author = "Hollin R. Wilkins"
|
35
|
+
p.email = "chromaticbum @nospam@ gmail.com"
|
36
|
+
p.ignore_pattern = ["tmp/*", "script/*"]
|
37
|
+
p.development_dependencies = []
|
38
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{acts_as_taggable_simple}
|
5
|
+
s.version = "0.0.2"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Hollin R. Wilkins"]
|
9
|
+
s.date = %q{2011-04-10}
|
10
|
+
s.description = %q{Adds acts_as_taggable to ActiveRecord::Model to easily tag objects, no extra fluff}
|
11
|
+
s.email = %q{chromaticbum @nospam@ gmail.com}
|
12
|
+
s.extra_rdoc_files = ["README.rdoc", "lib/active_record/acts/taggable.rb", "lib/acts_as_taggable_simple.rb", "lib/generators/acts_as_taggable_simple/migration/migration_generator.rb", "lib/generators/acts_as_taggable_simple/migration/templates/active_record/migration.rb", "lib/tag.rb", "lib/tag_list.rb", "lib/tagging.rb"]
|
13
|
+
s.files = ["Gemfile", "MIT-LICENSE", "README.rdoc", "Rakefile", "acts_as_taggable_simple.gemspec", "init.rb", "lib/active_record/acts/taggable.rb", "lib/acts_as_taggable_simple.rb", "lib/generators/acts_as_taggable_simple/migration/migration_generator.rb", "lib/generators/acts_as_taggable_simple/migration/templates/active_record/migration.rb", "lib/tag.rb", "lib/tag_list.rb", "lib/tagging.rb", "spec/acts_as_taggable_simple/acts_as_taggable_simple_spec.rb", "spec/acts_as_taggable_simple/spec_helper.rb", "spec/acts_as_taggable_simple/taggable_spec.rb", "spec/database.yml", "spec/models.rb", "spec/schema.rb", "spec/spec_helper.rb", "Manifest"]
|
14
|
+
s.homepage = %q{http://github.com/chromaticbum/acts_as_taggable_simple}
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Acts_as_taggable_simple", "--main", "README.rdoc"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = %q{acts_as_taggable_simple}
|
18
|
+
s.rubygems_version = %q{1.5.2}
|
19
|
+
s.summary = %q{Adds acts_as_taggable to ActiveRecord::Model to easily tag objects, no extra fluff}
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
s.specification_version = 3
|
23
|
+
|
24
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
25
|
+
else
|
26
|
+
end
|
27
|
+
else
|
28
|
+
end
|
29
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
module ActiveRecord #:nodoc:
|
2
|
+
module Acts #:nodoc:
|
3
|
+
|
4
|
+
# This +acts_as+ extension provides capabilities for adding and removing tags to ActiveRecord objects.
|
5
|
+
#
|
6
|
+
# Basic note example:
|
7
|
+
#
|
8
|
+
# class Note < ActiveRecord::Base
|
9
|
+
# acts_as_taggable
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
# note.tag_list = "rails css javascript"
|
13
|
+
# note.tag_list # returns ["rails", "css", "javascript"]
|
14
|
+
module Taggable
|
15
|
+
def self.included(base) #:nodoc:
|
16
|
+
base.send :extend, ClassMethods
|
17
|
+
end
|
18
|
+
|
19
|
+
# Provides one method: ActiveRecord::Base#acts_as_taggable.
|
20
|
+
module ClassMethods
|
21
|
+
|
22
|
+
# Call this method to make one of your models taggable.
|
23
|
+
#
|
24
|
+
# Future support for a :delimeter option for specifying the tag delimeter
|
25
|
+
def acts_as_taggable(options = {})
|
26
|
+
has_many :taggings, :as => :taggable, :class_name => "ActsAsTaggableSimple::Tagging"
|
27
|
+
has_many :tags, :through => :taggings, :as => :taggable, :class_name => "ActsAsTaggableSimple::Tag"
|
28
|
+
after_save :save_tags
|
29
|
+
|
30
|
+
class_eval <<-EOV
|
31
|
+
include ActiveRecord::Acts::Taggable::InstanceMethods
|
32
|
+
EOV
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# Provides methods to manage tags for a taggable model:
|
37
|
+
# * tag_list - returns an Array of tag String's
|
38
|
+
# * tag_list= - takes and Array of tag Strings or a String of tags separated by spaces
|
39
|
+
# * save_tags - updates the ActsAsTaggableSimple::Tag objects after saving a taggable model
|
40
|
+
module InstanceMethods
|
41
|
+
|
42
|
+
# Returns a TagList containing the String names of all tags for this object
|
43
|
+
def tag_list
|
44
|
+
self.instance_variable_get('@tag_list') || self.instance_variable_set('@tag_list', TagList.new(tags.map(&:name)))
|
45
|
+
end
|
46
|
+
|
47
|
+
# Sets the list of tags for this object
|
48
|
+
#
|
49
|
+
# +list+ can be either a String of tags separated by spaces or an Array of String's
|
50
|
+
def tag_list=(list)
|
51
|
+
self.instance_variable_set('@tag_list', list.is_a?(String) ? TagList.new(list.split(" ")) : list)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Called right before saving the model to update the associated ActsAsTaggableSimple::Tag objects
|
55
|
+
# Once saved this also necessarily updates the ActsAsTaggableSimple::Tagging objects for this record
|
56
|
+
def save_tags
|
57
|
+
self.tags = ActsAsTaggableSimple::Tag.find_or_create_all_tags(self.tag_list)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
ActiveRecord::Base.class_eval do
|
65
|
+
include ActiveRecord::Acts::Taggable
|
66
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module ActsAsTaggableSimple #:nodoc:
|
2
|
+
module Generators #:nodoc:
|
3
|
+
|
4
|
+
# Generates a migration file for migrating two tables into the database for acts_as_taggable_simple to function:
|
5
|
+
# * tags
|
6
|
+
# * taggings
|
7
|
+
class MigrationGenerator < Rails::Generators::Base
|
8
|
+
include Rails::Generators::Migration
|
9
|
+
|
10
|
+
desc "Generates migration for Tag and Tagging models"
|
11
|
+
|
12
|
+
def self.orm
|
13
|
+
Rails::Generators.options[:rails][:orm]
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.source_root
|
17
|
+
File.join(File.dirname(__FILE__), 'templates', (orm.to_s unless orm.class.eql?(String)))
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.orm_has_migration?
|
21
|
+
[:active_record].include? orm
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.next_migration_number(path)
|
25
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
26
|
+
end
|
27
|
+
|
28
|
+
def create_migration_file
|
29
|
+
if self.class.orm_has_migration?
|
30
|
+
migration_template 'migration.rb', 'db/migrate/acts_as_taggable_simple_migration'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class ActsAsTaggableSimpleMigration < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :tags do |t|
|
4
|
+
t.string :name
|
5
|
+
end
|
6
|
+
|
7
|
+
add_index :tags, :name, :unique => true
|
8
|
+
|
9
|
+
create_table :taggings do |t|
|
10
|
+
t.references :taggable, :polymorphic => true
|
11
|
+
t.references :tag
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.down
|
16
|
+
drop_table :tagging
|
17
|
+
|
18
|
+
drop_table :tags
|
19
|
+
end
|
20
|
+
end
|
data/lib/tag.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
module ActsAsTaggableSimple #:nodoc:
|
2
|
+
|
3
|
+
# ActiveRecord model for storing a tag in the database.
|
4
|
+
#
|
5
|
+
# Tags have only one attribute: +name+, which is just the text representation of the tag.
|
6
|
+
# +name+ must be unique for each instance.
|
7
|
+
class Tag < ::ActiveRecord::Base
|
8
|
+
attr_accessible :name
|
9
|
+
|
10
|
+
validates_uniqueness_of :name
|
11
|
+
|
12
|
+
# Finds or creates all of the tags contained in +list+ and returns them.
|
13
|
+
#
|
14
|
+
# +list+ is an Array of String's containing the +name+'s of the desired tags.
|
15
|
+
# If an ActsAsTaggableSimple::Tag does not yet exist for a tag, then it is created, otherwise the already existing tag is used.
|
16
|
+
#
|
17
|
+
# Empty database example:
|
18
|
+
# tags = ActsAsTaggableSimple::Tag.find_or_create_all_tags(%w{rails css html})
|
19
|
+
# Will create 3 new ActsAsTaggableSimple::Tag objects and save them to the database.
|
20
|
+
#
|
21
|
+
# If +rails+, +css+, and +html+ tags already exist in the database, then the following example will only create 1 new tag.
|
22
|
+
# tags = ActsAsTaggableSimple::Tag.find_or_create_all_tags(%w{rails css html javascript})
|
23
|
+
def self.find_or_create_all_tags(list)
|
24
|
+
existing_tags = Tag.where(:name => list)
|
25
|
+
create_tags_list = list - existing_tags.map(&:name)
|
26
|
+
|
27
|
+
create_tags = create_tags_list.collect do |tag|
|
28
|
+
Tag.create!(:name => tag)
|
29
|
+
end
|
30
|
+
|
31
|
+
create_tags + existing_tags.all
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/tag_list.rb
ADDED
data/lib/tagging.rb
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
module ActsAsTaggableSimple #:nodoc:
|
2
|
+
|
3
|
+
# ActiveRecord model for storing a join between a taggable object and an ActsAsTaggableSimple::Tag object.
|
4
|
+
class Tagging < ::ActiveRecord::Base
|
5
|
+
belongs_to :taggable, :polymorphic => true
|
6
|
+
belongs_to :tag, :class_name => "ActsAsTaggableSimple::Tag"
|
7
|
+
end
|
8
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Acts As Taggable Simple" do
|
4
|
+
describe "Taggable Method Generation" do
|
5
|
+
before :each do
|
6
|
+
clean_database!
|
7
|
+
@taggable = TaggableModel.new
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should generate a tag_list and tag_list= method" do
|
11
|
+
@taggable.should respond_to(:tag_list, :tag_list=)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require File.expand_path("../../spec_helper", __FILE__)
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Taggable" do
|
4
|
+
before :each do
|
5
|
+
clean_database!
|
6
|
+
@taggable = TaggableModel.new
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should be able to create tags" do
|
10
|
+
@taggable.tag_list = "tag2 tag3 tag1"
|
11
|
+
@taggable.tag_list.should be_kind_of(Array)
|
12
|
+
|
13
|
+
lambda {
|
14
|
+
@taggable.save
|
15
|
+
}.should change(ActsAsTaggableSimple::Tag, :count).by(3)
|
16
|
+
|
17
|
+
@taggable.tag_list.sort.should == %w(tag2 tag3 tag1).sort
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should not create tags that already exist and create tags that do not yet exist when saving" do
|
21
|
+
@taggable.tag_list = "tag2 tag3 tag1"
|
22
|
+
@taggable.save
|
23
|
+
|
24
|
+
taggable1 = TaggableModel.new(:tag_list => "tag2 tag1 rails css")
|
25
|
+
taggable2 = TaggableModel.new(:tag_list => "tag2 tag1 rails css")
|
26
|
+
|
27
|
+
lambda {
|
28
|
+
taggable1.save
|
29
|
+
}.should change(ActsAsTaggableSimple::Tag, :count).by(2)
|
30
|
+
|
31
|
+
lambda {
|
32
|
+
taggable2.save
|
33
|
+
}.should change(ActsAsTaggableSimple::Tag, :count).by(0)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should delete old taggings when saving" do
|
37
|
+
@taggable.tag_list = "ruby rails css"
|
38
|
+
@taggable.save
|
39
|
+
|
40
|
+
@taggable.tag_list = "ruby rails"
|
41
|
+
|
42
|
+
lambda {
|
43
|
+
@taggable.save
|
44
|
+
}.should change(@taggable.taggings, :count).by(-1)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should retain unchanged taggings when saving" do
|
48
|
+
@taggable.tag_list = "ruby rails css"
|
49
|
+
@taggable.save
|
50
|
+
old_taggings = @taggable.taggings
|
51
|
+
|
52
|
+
@taggable.tag_list = "ruby rails"
|
53
|
+
@taggable.save
|
54
|
+
|
55
|
+
@taggable.taggings.should == old_taggings
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should reload tags whenever tag list is changed and then model is saved" do
|
59
|
+
@taggable.tag_list = "ruby rails css"
|
60
|
+
@taggable.tags.empty?.should be_true
|
61
|
+
|
62
|
+
@taggable.save
|
63
|
+
@taggable.tags.empty?.should be_false
|
64
|
+
end
|
65
|
+
end
|
data/spec/database.yml
ADDED
data/spec/models.rb
ADDED
data/spec/schema.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
ActiveRecord::Schema.define(:version => 0) do
|
2
|
+
create_table :tags do |t|
|
3
|
+
t.string :name
|
4
|
+
end
|
5
|
+
|
6
|
+
add_index :tags, :name, :unique => true
|
7
|
+
|
8
|
+
create_table :taggings do |t|
|
9
|
+
t.references :taggable, :polymorphic => true
|
10
|
+
t.references :tag
|
11
|
+
end
|
12
|
+
|
13
|
+
create_table :taggable_models do |t|
|
14
|
+
end
|
15
|
+
|
16
|
+
create_table :untaggable_models do |t|
|
17
|
+
end
|
18
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
require 'active_support'
|
6
|
+
require 'active_record'
|
7
|
+
require 'logger'
|
8
|
+
|
9
|
+
require 'acts_as_taggable_simple'
|
10
|
+
|
11
|
+
ENV["DB"] ||= "sqlite3"
|
12
|
+
|
13
|
+
database_yml = File.expand_path(File.join(File.dirname(__FILE__), "database.yml"))
|
14
|
+
raise "Please create test/database.yml file" if not File.exists? database_yml
|
15
|
+
|
16
|
+
active_record_configuration = YAML.load_file(database_yml)[ENV["DB"]]
|
17
|
+
|
18
|
+
ActiveRecord::Base.establish_connection(active_record_configuration)
|
19
|
+
ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), "debug.log"))
|
20
|
+
|
21
|
+
ActiveRecord::Base.silence do
|
22
|
+
ActiveRecord::Migration.verbose = false
|
23
|
+
|
24
|
+
load(File.dirname(__FILE__) + "/schema.rb")
|
25
|
+
load(File.dirname(__FILE__) + "/models.rb")
|
26
|
+
end
|
27
|
+
|
28
|
+
def clean_database!
|
29
|
+
models = [ActsAsTaggableSimple::Tag, ActsAsTaggableSimple::Tagging, TaggableModel, UntaggableModel]
|
30
|
+
models.each do |model|
|
31
|
+
ActiveRecord::Base.connection.execute "DELETE FROM #{model.table_name}"
|
32
|
+
end
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_taggable_simple
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Hollin R. Wilkins
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-04-10 00:00:00.000000000 +03:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
description: Adds acts_as_taggable to ActiveRecord::Model to easily tag objects, no
|
16
|
+
extra fluff
|
17
|
+
email: chromaticbum @nospam@ gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files:
|
21
|
+
- README.rdoc
|
22
|
+
- lib/active_record/acts/taggable.rb
|
23
|
+
- lib/acts_as_taggable_simple.rb
|
24
|
+
- lib/generators/acts_as_taggable_simple/migration/migration_generator.rb
|
25
|
+
- lib/generators/acts_as_taggable_simple/migration/templates/active_record/migration.rb
|
26
|
+
- lib/tag.rb
|
27
|
+
- lib/tag_list.rb
|
28
|
+
- lib/tagging.rb
|
29
|
+
files:
|
30
|
+
- Gemfile
|
31
|
+
- MIT-LICENSE
|
32
|
+
- README.rdoc
|
33
|
+
- Rakefile
|
34
|
+
- acts_as_taggable_simple.gemspec
|
35
|
+
- init.rb
|
36
|
+
- lib/active_record/acts/taggable.rb
|
37
|
+
- lib/acts_as_taggable_simple.rb
|
38
|
+
- lib/generators/acts_as_taggable_simple/migration/migration_generator.rb
|
39
|
+
- lib/generators/acts_as_taggable_simple/migration/templates/active_record/migration.rb
|
40
|
+
- lib/tag.rb
|
41
|
+
- lib/tag_list.rb
|
42
|
+
- lib/tagging.rb
|
43
|
+
- spec/acts_as_taggable_simple/acts_as_taggable_simple_spec.rb
|
44
|
+
- spec/acts_as_taggable_simple/spec_helper.rb
|
45
|
+
- spec/acts_as_taggable_simple/taggable_spec.rb
|
46
|
+
- spec/database.yml
|
47
|
+
- spec/models.rb
|
48
|
+
- spec/schema.rb
|
49
|
+
- spec/spec_helper.rb
|
50
|
+
- Manifest
|
51
|
+
has_rdoc: true
|
52
|
+
homepage: http://github.com/chromaticbum/acts_as_taggable_simple
|
53
|
+
licenses: []
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options:
|
56
|
+
- --line-numbers
|
57
|
+
- --inline-source
|
58
|
+
- --title
|
59
|
+
- Acts_as_taggable_simple
|
60
|
+
- --main
|
61
|
+
- README.rdoc
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.2'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project: acts_as_taggable_simple
|
78
|
+
rubygems_version: 1.5.2
|
79
|
+
signing_key:
|
80
|
+
specification_version: 3
|
81
|
+
summary: Adds acts_as_taggable to ActiveRecord::Model to easily tag objects, no extra
|
82
|
+
fluff
|
83
|
+
test_files: []
|