simple_tag 0.0.1

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/.gitignore ADDED
@@ -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/.project ADDED
@@ -0,0 +1,18 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <projectDescription>
3
+ <name>simple_tag</name>
4
+ <comment></comment>
5
+ <projects>
6
+ </projects>
7
+ <buildSpec>
8
+ <buildCommand>
9
+ <name>com.aptana.ide.core.unifiedBuilder</name>
10
+ <arguments>
11
+ </arguments>
12
+ </buildCommand>
13
+ </buildSpec>
14
+ <natures>
15
+ <nature>org.radrails.rails.core.railsnature</nature>
16
+ <nature>com.aptana.ruby.core.rubynature</nature>
17
+ </natures>
18
+ </projectDescription>
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple_tag.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 TODO: Write your name
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.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # SimpleTag
2
+
3
+ Lightweight tagging for Rails 3.
4
+
5
+ ## Installation
6
+
7
+ In your Gemfile:
8
+
9
+ gem 'simple_tag'
10
+
11
+ At the command line:
12
+
13
+ $ bundle install
14
+
15
+ Do migration:
16
+
17
+ rails generate simple_tag:migration
18
+ rake db:migrate
19
+
20
+ ## Usage
21
+
22
+ In your model:
23
+
24
+ is_taggable
25
+
26
+ Make tags:
27
+
28
+ obj.add_tags(["tag1", "tag2"])
29
+ obj.add_tags("tag3")
30
+
31
+ Delete tags:
32
+
33
+ obj.remove_tags(["tag1", "tag2"])
34
+ obj.remove_tags("tag3")
35
+
36
+ Find tagged items:
37
+
38
+ TaggableModel.tagged_with(["tag1", "tag2"])
39
+ TaggableModel.tagged_with("tag3")
40
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,39 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ module SimpleTag
5
+ class MigrationGenerator < Rails::Generators::Base
6
+ include Rails::Generators::Migration
7
+
8
+ desc "Generates migration for Tag and Tagging models"
9
+
10
+ def self.orm
11
+ Rails::Generators.options[:rails][:orm]
12
+ end
13
+
14
+ def self.source_root
15
+ File.join(File.dirname(__FILE__), 'templates', (orm.to_s unless orm.class.eql?(String)) )
16
+ end
17
+
18
+ def self.orm_has_migration?
19
+ [:active_record].include? orm
20
+ end
21
+
22
+ def self.next_migration_number(dirname)
23
+ if ActiveRecord::Base.timestamped_migrations
24
+ migration_number = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
25
+ migration_number += 1
26
+ migration_number.to_s
27
+ else
28
+ "%.3d" % (current_migration_number(dirname) + 1)
29
+ end
30
+ end
31
+
32
+ def create_migration_file
33
+ if self.class.orm_has_migration?
34
+ migration_template 'migration.rb', 'db/migrate/simple_tag_migration'
35
+ end
36
+ end
37
+ end
38
+ end
39
+
@@ -0,0 +1,24 @@
1
+ class SimpleTagMigration < ActiveRecord::Migration
2
+
3
+ def self.up
4
+ create_table :tags do |t|
5
+ t.string :name
6
+ end
7
+
8
+ create_table :taggings do |t|
9
+ t.references :tag
10
+ t.references :taggable, :polymorphic => true
11
+
12
+ t.datetime :created_at
13
+ end
14
+
15
+ add_index :taggings, :tag_id
16
+ add_index :taggings, [:taggable_id, :taggable_type]
17
+ end
18
+
19
+ def self.down
20
+ drop_table :taggings
21
+ drop_table :tags
22
+ end
23
+
24
+ end
data/lib/simple_tag.rb ADDED
@@ -0,0 +1,10 @@
1
+ require "active_record"
2
+
3
+ require "simple_tag/version"
4
+ require "simple_tag/taggable"
5
+ require "simple_tag/tagging"
6
+ require "simple_tag/tag"
7
+
8
+ if defined?(ActiveRecord::Base)
9
+ ActiveRecord::Base.extend SimpleTag::Taggable
10
+ end
@@ -0,0 +1,9 @@
1
+ module SimpleTag
2
+ class Tag < ActiveRecord::Base
3
+ has_many :taggings, :dependent => :destroy, :class_name => 'SimpleTag::Tagging'
4
+ attr_accessible :name
5
+
6
+ validates_presence_of :name
7
+ validates_uniqueness_of :name
8
+ end
9
+ end
@@ -0,0 +1,69 @@
1
+ module SimpleTag
2
+ module Taggable
3
+ def is_taggable
4
+ class_eval do
5
+ has_many :taggings, :as => :taggable, :dependent => :destroy, :class_name => "SimpleTag::Tagging"
6
+ has_many :tags, :through => :taggings, :class_name => "SimpleTag::Tag"
7
+
8
+ include InstanceMethods
9
+
10
+ def self.tagged_with(names)
11
+ names = format_tag_names(names)
12
+ if names.blank?
13
+ return []
14
+ end
15
+ query_tag_ids = Tag.where(:name => names).map {|t| t.id }
16
+ query_tag_count = query_tag_ids.length
17
+ taggable_ids = joins(:tags).where(:tags => {:id => query_tag_ids}).select("#{table_name}.id").map(&:id)
18
+ taggable_id_count = Hash.new(0)
19
+ taggable_ids.each {|t| taggable_id_count[t] += 1 }
20
+ taggable_id_count.select! {|k,v| v == query_tag_count }
21
+ find(taggable_id_count.keys)
22
+ end
23
+
24
+ private
25
+
26
+ def self.format_tag_names(names)
27
+ if names.is_a?(String)
28
+ names = [names]
29
+ end
30
+ if !names.is_a?(Array)
31
+ return nil
32
+ end
33
+ names = names.reject {|n| n.blank? or !n.is_a?(String) }
34
+ end
35
+
36
+ end
37
+ end
38
+
39
+ module InstanceMethods
40
+
41
+ def add_tags(names)
42
+ names = self.class.format_tag_names(names)
43
+ if names.blank?
44
+ return []
45
+ end
46
+ new_tags = []
47
+ names.each do |n|
48
+ new_tags << Tag.find_or_create_by_name(n)
49
+ end
50
+ self.tags += new_tags
51
+ end
52
+
53
+ def remove_tags(names)
54
+ names = self.class.format_tag_names(names)
55
+ if names.blank?
56
+ return []
57
+ end
58
+ rem_tags = []
59
+ names.each do |n|
60
+ rem_tags << Tag.find_by_name(n)
61
+ end
62
+ self.tags -= rem_tags
63
+ end
64
+
65
+ end
66
+
67
+ end
68
+
69
+ end
@@ -0,0 +1,15 @@
1
+ module SimpleTag
2
+ class Tagging < ::ActiveRecord::Base
3
+
4
+ attr_accessible :tag, :tag_id, :taggable, :taggable_type, :taggable_id
5
+
6
+ belongs_to :tag, :class_name => 'SimpleTag::Tag'
7
+ belongs_to :taggable, :polymorphic => true
8
+
9
+ validates_presence_of :tag_id
10
+ validates_presence_of :taggable_id
11
+ validates_presence_of :taggable_type
12
+
13
+ validates_uniqueness_of :tag_id, :scope => [ :taggable_type, :taggable_id ]
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ module SimpleTag
2
+ VERSION = "0.0.1"
3
+ end
4
+
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/simple_tag/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Alexey"]
6
+ gem.email = "nickname4ever@googlemail.com"
7
+ gem.description = ""
8
+ gem.summary = "Lightweight tagging for Rails 3"
9
+ gem.homepage = "https://github.com/nicknameforever/simple_tag"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "simple_tag"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = SimpleTag::VERSION
17
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_tag
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alexey
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-06 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ''
15
+ email: nickname4ever@googlemail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - .gitignore
21
+ - .project
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - lib/generators/simple_tag/migration/migration_generator.rb
27
+ - lib/generators/simple_tag/migration/templates/active_record/migration.rb
28
+ - lib/simple_tag.rb
29
+ - lib/simple_tag/tag.rb
30
+ - lib/simple_tag/taggable.rb
31
+ - lib/simple_tag/tagging.rb
32
+ - lib/simple_tag/version.rb
33
+ - simple_tag.gemspec
34
+ homepage: https://github.com/nicknameforever/simple_tag
35
+ licenses: []
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 1.8.19
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Lightweight tagging for Rails 3
58
+ test_files: []