culturecode-acts_as_taggable 0.2.3

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 805daa96b56881db74f2db609fbdbe17c8b398b0
4
+ data.tar.gz: 97f49a0d3a0e429ad1f28c774eb71a379d49fb44
5
+ SHA512:
6
+ metadata.gz: f4bb21307f9e5e2981b02f98296a8a451f0cbb93619f48f646adc0b09905b19bc9784da654ecbda7450ee494a3c7a4838690e33e8922815199207183d681519b
7
+ data.tar.gz: 2723ee05f50b1a646b7b4ae0a4b505dee171ca0637cea8d3230d01e4b3ebe539705dfee0091e4c59527be01b1a7fa08c910e98062ab3858ef8c96db002de8c2d
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ == ActsAsTaggable
2
+
3
+ Simple tagging.
@@ -0,0 +1,47 @@
1
+ module ActsAsTaggable
2
+ module ActMethod #:nodoc:
3
+ def acts_as_taggable(options = {})
4
+ has_many :taggings, :as => :taggable
5
+ has_many :tags, :through => :taggings
6
+
7
+ extend ActsAsTaggable::ClassMethods
8
+ include ActsAsTaggable::InstanceMethods
9
+ end
10
+ end
11
+
12
+ module ClassMethods
13
+ # TODO: tagged_with_all
14
+
15
+ def tagged_with_any(*args)
16
+ args.flatten! # Allow an array of tags to be passed in
17
+
18
+ joins(:tags).where(:tags => {:name => args.collect {|tag_name| Tag.sanitize_name(tag_name) } }).uniq
19
+ end
20
+
21
+ # Make it possible to ask for tags on a scoped Taggable relation. e.g. Users.online.tags
22
+ def tags
23
+ Tag.joins(:taggings).where(:taggings => {:taggable_type => self, :taggable_id => all}).group('tags.id').select("tags.*, COUNT(*) AS count")
24
+ end
25
+ end
26
+
27
+ module InstanceMethods
28
+ TAG_DELIMITER = ','
29
+
30
+ def acts_like_taggable?
31
+ true
32
+ end
33
+
34
+ def tags_list=(tag_string)
35
+ self.tags = tag_string.to_s.split(TAG_DELIMITER).collect{|tag_name| Tag.find_or_create_by!(:name => Tag.sanitize_name(tag_name)) }
36
+ end
37
+
38
+ def tags_list
39
+ tag_names.join(TAG_DELIMITER + ' ')
40
+ end
41
+
42
+ def tag_names
43
+ tags.collect(&:name) # don't use pluck since we want to use the cached association
44
+ end
45
+ end
46
+ end
47
+
@@ -0,0 +1,21 @@
1
+ class Tag < ActiveRecord::Base
2
+ has_many :taggings
3
+ default_scope lambda { order(:name) }
4
+
5
+ validates_presence_of :name
6
+ before_save :sanitize_name
7
+
8
+ def self.sanitize_name(name)
9
+ name.to_s.strip.squeeze(' ').downcase
10
+ end
11
+
12
+ def to_s
13
+ self.name
14
+ end
15
+
16
+ private
17
+
18
+ def sanitize_name
19
+ self.name = self.class.sanitize_name(self.name)
20
+ end
21
+ end
@@ -0,0 +1,4 @@
1
+ class Tagging < ActiveRecord::Base
2
+ belongs_to :tag
3
+ belongs_to :taggable, :polymorphic => true
4
+ end
@@ -0,0 +1,5 @@
1
+ require 'acts_as_taggable/acts_as_taggable'
2
+ require 'acts_as_taggable/tag'
3
+ require 'acts_as_taggable/tagging'
4
+
5
+ ActiveRecord::Base.extend ActsAsTaggable::ActMethod
@@ -0,0 +1,23 @@
1
+ class ActsAsTaggableMigrationGenerator < Rails::Generators::Base
2
+ def create_migration_file
3
+ create_file "db/migrations/initializer.rb", <<-EOV
4
+ class ActsAsTaggableTable < ActiveRecord::Migration
5
+ def change
6
+ create_table :tags do |t|
7
+ t.string :name
8
+ t.string :tag_type
9
+ end
10
+
11
+ create_table :taggings do |t|
12
+ t.belongs_to :tag
13
+ t.belongs_to :taggable, :polymorphic => true
14
+ end
15
+
16
+ add_index :tags, [:name, :tag_type], :unique => true
17
+ add_index :taggings, [:taggable_type, :taggable_id]
18
+ add_index :taggings, :tag_id
19
+ end
20
+ end
21
+ EOV
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: culturecode-acts_as_taggable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.3
5
+ platform: ruby
6
+ authors:
7
+ - Nicholas Jakobsen
8
+ - Ryan Wallace
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ~>
19
+ - !ruby/object:Gem::Version
20
+ version: '4.0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ version: '4.0'
28
+ description:
29
+ email: contact@culturecode.ca
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/acts_as_taggable/acts_as_taggable.rb
35
+ - lib/acts_as_taggable/tag.rb
36
+ - lib/acts_as_taggable/tagging.rb
37
+ - lib/acts_as_taggable.rb
38
+ - lib/generators/migration_generator.rb
39
+ - README.rdoc
40
+ homepage: http://github.com/culturecode/acts_as_taggable
41
+ licenses: []
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.0.5
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Simple record tagging
63
+ test_files: []