acts_as_keywordable 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in acts_as_keywordable.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 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,32 @@
1
+ # ActsAsKeywordable
2
+
3
+ Add keywords (like tags) polymorphically to any AR model
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'acts_as_keywordable'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install acts_as_keywordable
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
30
+ =======
31
+ acts_as_keywordable
32
+ ===================
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'acts_as_keywordable/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "acts_as_keywordable"
8
+ spec.version = ActsAsKeywordable::VERSION
9
+ spec.authors = ["Jake Varghese (github.com/jake3030)"]
10
+ spec.email = ["jake3030@no-reply.github.com"]
11
+ spec.description = %q{real quick gem to add keywords across multiple models}
12
+ spec.summary = %q{see description}
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_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency 'rspec', '~> 2.6.0'
24
+ spec.add_development_dependency 'ruby-debug'
25
+
26
+ end
@@ -0,0 +1,91 @@
1
+ require "acts_as_keywordable/version"
2
+ require "acts_as_keywordable/keyword"
3
+ require "acts_as_keywordable/keywording"
4
+
5
+ module ActiveRecord
6
+ module Acts #:nodoc:
7
+ module Keywordable #:nodoc:
8
+ def self.included(base)
9
+ base.extend(ClassMethods)
10
+ end
11
+
12
+ module ClassMethods
13
+ def acts_as_keywordable(options = {})
14
+ class_attribute(:acts_as_keywordable_options, {
15
+ :keywordable_type => self.base_class.name.to_s,
16
+ :from => options[:from]
17
+ })
18
+
19
+
20
+ has_many :keywordings, :as => :keywordable, :dependent => :destroy
21
+ has_many :keywords, :through => :keywordings
22
+
23
+ include ActiveRecord::Acts::Keywordable::InstanceMethods
24
+ extend ActiveRecord::Acts::Keywordable::SingletonMethods
25
+ end
26
+ end
27
+
28
+ module SingletonMethods
29
+ def find_tagged_with(list)
30
+ sql_str = %Q(
31
+ SELECT #{table_name}.* FROM #{table_name}, keywords, keywordings
32
+ WHERE #{table_name}.#{primary_key} = taggings.keywordable_id
33
+ AND keywordings.keywordable_type = ?
34
+ AND keywordings.keyword_id = keywords.id AND keywords.name IN (?)
35
+
36
+ )
37
+ find_by_sql([sql_str, acts_as_taggable_options[:keywordable_type], list])
38
+ end
39
+
40
+ def tags_count(options)
41
+ sql = "SELECT keywords.id AS id, keywords.name AS name, COUNT(*) AS count FROM keywords, keywordings, #{table_name} "
42
+ sql << "WHERE keywordings.keywordable_id = #{table_name}.#{primary_key} AND keywordings.keyword_id = keywords.id "
43
+ sql << "AND #{sanitize_sql(options[:conditions])} " if options[:conditions]
44
+ sql << "GROUP BY keywords.name "
45
+ sql << "HAVING count #{options[:count]} " if options[:count]
46
+ sql << "ORDER BY #{options[:order]} " if options[:order]
47
+ sql << "LIMIT #{options[:limit]} " if options[:limit]
48
+ find_by_sql(sql)
49
+ end
50
+ end
51
+
52
+ module InstanceMethods
53
+ def tag_with(list)
54
+ Keyword.transaction do
55
+ keywordings.destroy_all
56
+
57
+ Keyword.parse(list).each do |name|
58
+ if acts_as_keywordable_options[:from]
59
+ send(acts_as_keywordable_options[:from]).keywords.find_or_create_by_name(name).on(self)
60
+ else
61
+ Keyword.find_or_create_by_name(name).on(self)
62
+ end
63
+ end
64
+ end
65
+ end
66
+
67
+ def add_keywords(list)
68
+ Keyword.transaction do
69
+ keywordings.destroy_all
70
+
71
+ Keyword.parse(list).each do |name|
72
+ if acts_as_keywordable_options[:from]
73
+ send(acts_as_keywordable_options[:from]).keywords.find_or_create_by_name(name).on(self)
74
+ else
75
+ Keyword.find_or_create_by_name(name).on(self)
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ def tag_list
82
+ #keywords.collect { |tag| righttag.name.include?(" ") ? "'#{tag.name}'" : tag.name }.join(" ")
83
+ end
84
+
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+
91
+ ActiveRecord::Base.send(:include, ActiveRecord::Acts::Keywordable)
@@ -0,0 +1,38 @@
1
+ class Keyword < ActiveRecord::Base
2
+ has_many :keywordings
3
+
4
+ def self.parse(list)
5
+ keyword_names = []
6
+
7
+
8
+ keyword_names = list.split(/[\r\n]/).uniq.reject {|c| c == ""}
9
+
10
+ # strip whitespace from the names
11
+ keyword_names = keyword_names.map { |t| t.strip }
12
+
13
+ # delete any blank tag names
14
+ keyword_names = keyword_names.delete_if { |t| t.empty? }
15
+
16
+ return keyword_names
17
+ end
18
+
19
+ def tagged
20
+ @tagged ||= keywordings.collect { |keywording| keywording.keywordable }
21
+ end
22
+
23
+ def on(keywordable)
24
+ keywordings.create :keywordable => keywordable
25
+ end
26
+
27
+ def also_on(keywordable)
28
+ keywordings.find_all_by_keywordable_type(keywordable.to_s)
29
+ end
30
+
31
+ def ==(comparison_object)
32
+ super || name == comparison_object.to_s
33
+ end
34
+
35
+ def to_s
36
+ name
37
+ end
38
+ end
@@ -0,0 +1,12 @@
1
+ class Keywording < ActiveRecord::Base
2
+ belongs_to :keyword
3
+ belongs_to :keywordable, :polymorphic => true
4
+
5
+ def self.tagged_class(keywordable)
6
+ ActiveRecord::Base.send(:class_name_of_active_record_descendant, keywordable.class).to_s
7
+ end
8
+
9
+ def self.find_taggable(tagged_class, tagged_id)
10
+ tagged_class.constantize.find(tagged_id)
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module ActsAsKeywordable
2
+ VERSION = "0.0.8"
3
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_keywordable
3
+ version: !ruby/object:Gem::Version
4
+ hash: 15
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 8
10
+ version: 0.0.8
11
+ platform: ruby
12
+ authors:
13
+ - Jake Varghese (github.com/jake3030)
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2013-11-05 00:00:00 -06:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ prerelease: false
23
+ version_requirements: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 9
29
+ segments:
30
+ - 1
31
+ - 3
32
+ version: "1.3"
33
+ requirement: *id001
34
+ name: bundler
35
+ type: :development
36
+ - !ruby/object:Gem::Dependency
37
+ prerelease: false
38
+ version_requirements: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ requirement: *id002
48
+ name: rake
49
+ type: :development
50
+ - !ruby/object:Gem::Dependency
51
+ prerelease: false
52
+ version_requirements: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ hash: 23
58
+ segments:
59
+ - 2
60
+ - 6
61
+ - 0
62
+ version: 2.6.0
63
+ requirement: *id003
64
+ name: rspec
65
+ type: :development
66
+ - !ruby/object:Gem::Dependency
67
+ prerelease: false
68
+ version_requirements: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ requirement: *id004
78
+ name: ruby-debug
79
+ type: :development
80
+ description: real quick gem to add keywords across multiple models
81
+ email:
82
+ - jake3030@no-reply.github.com
83
+ executables: []
84
+
85
+ extensions: []
86
+
87
+ extra_rdoc_files: []
88
+
89
+ files:
90
+ - .gitignore
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - acts_as_keywordable.gemspec
96
+ - lib/acts_as_keywordable.rb
97
+ - lib/acts_as_keywordable/keyword.rb
98
+ - lib/acts_as_keywordable/keywording.rb
99
+ - lib/acts_as_keywordable/version.rb
100
+ has_rdoc: true
101
+ homepage: ""
102
+ licenses:
103
+ - MIT
104
+ post_install_message:
105
+ rdoc_options: []
106
+
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ hash: 3
115
+ segments:
116
+ - 0
117
+ version: "0"
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ hash: 3
124
+ segments:
125
+ - 0
126
+ version: "0"
127
+ requirements: []
128
+
129
+ rubyforge_project:
130
+ rubygems_version: 1.4.1
131
+ signing_key:
132
+ specification_version: 3
133
+ summary: see description
134
+ test_files: []
135
+