fav_star 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ *.gem
@@ -0,0 +1,3 @@
1
+ 2010-08-15
2
+ ==========
3
+ * Branched FavStar off of ThumbsUp: favourites instead of votes.
data/MIT-LICENSE ADDED
@@ -0,0 +1,66 @@
1
+ Copyright (c) 2010 Brady Bouchard (ldawn.com)
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.
21
+
22
+ Major portions of this package were adapted from VoteFu, which is subject to the same license. Here is the original copyright notice for VoteFu:
23
+
24
+ Copyright (c) 2008 Peter Jackson (peteonrails.com)
25
+
26
+ Permission is hereby granted, free of charge, to any person obtaining
27
+ a copy of this software and associated documentation files (the
28
+ "Software"), to deal in the Software without restriction, including
29
+ without limitation the rights to use, copy, modify, merge, publish,
30
+ distribute, sublicense, and/or sell copies of the Software, and to
31
+ permit persons to whom the Software is furnished to do so, subject to
32
+ the following conditions:
33
+
34
+ The above copyright notice and this permission notice shall be
35
+ included in all copies or substantial portions of the Software.
36
+
37
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
38
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
39
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
40
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
41
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
42
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
43
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
44
+
45
+ Major portions of this package were adapted from ActsAsFaveable, which is subject to the same license. Here is the original copyright notice for ActsAsFaveable:
46
+
47
+ Copyright (c) 2006 Cosmin Radoi
48
+
49
+ Permission is hereby granted, free of charge, to any person obtaining
50
+ a copy of this software and associated documentation files (the
51
+ "Software"), to deal in the Software without restriction, including
52
+ without limitation the rights to use, copy, modify, merge, publish,
53
+ distribute, sublicense, and/or sell copies of the Software, and to
54
+ permit persons to whom the Software is furnished to do so, subject to
55
+ the following conditions:
56
+
57
+ The above copyright notice and this permission notice shall be
58
+ included in all copies or substantial portions of the Software.
59
+
60
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
61
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
62
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
63
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
64
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
65
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
66
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,15 @@
1
+ FavStar
2
+ =======
3
+
4
+ Run the generator to add the migration and model to your project:
5
+
6
+ rails g fav_star
7
+ rake db:migrate
8
+
9
+ Add to your users / voters:
10
+
11
+ acts_as_faver
12
+
13
+ Add to your faveable models:
14
+
15
+ acts_as_faveable
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ # encoding: UTF-8
2
+ require 'rubygems'
3
+ require 'rake'
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |gem|
8
+ gem.name = "fav_star"
9
+ gem.summary = "Mark your ActiveRecord models as favourites with only a couple of lines of code."
10
+ gem.description = "fav_star provides a fairly straightforward module that allows you to keep track of which models your users have favourited."
11
+ gem.email = "brady@ldawn.com"
12
+ gem.homepage = "http://github.com/brady8/favstar"
13
+ gem.authors = ["Brady Bouchard"]
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
data/fav_star.gemspec ADDED
@@ -0,0 +1,49 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{fav_star}
8
+ s.version = "0.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Brady Bouchard"]
12
+ s.date = %q{2010-08-19}
13
+ s.description = %q{fav_star provides a fairly straightforward module that allows you to keep track of which models your users have favourited.}
14
+ s.email = %q{brady@ldawn.com}
15
+ s.extra_rdoc_files = [
16
+ "README.markdown"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "CHANGELOG.markdown",
21
+ "MIT-LICENSE",
22
+ "README.markdown",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "fav_star.gemspec",
26
+ "lib/acts_as_faveable.rb",
27
+ "lib/acts_as_faver.rb",
28
+ "lib/fav_star.rb",
29
+ "lib/generators/fav_star/fav_star_generator.rb",
30
+ "lib/generators/fav_star/templates/fave.rb",
31
+ "lib/generators/fav_star/templates/migration.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/brady8/favstar}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.7}
37
+ s.summary = %q{Mark your ActiveRecord models as favourites with only a couple of lines of code.}
38
+
39
+ if s.respond_to? :specification_version then
40
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
41
+ s.specification_version = 3
42
+
43
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44
+ else
45
+ end
46
+ else
47
+ end
48
+ end
49
+
@@ -0,0 +1,41 @@
1
+ module FavStar
2
+ module ActsAsFaveable #:nodoc:
3
+
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def acts_as_faveable
10
+ has_many :favs, :as => :faveable, :dependent => :nullify
11
+
12
+ include FavStar::ActsAsFaveable::InstanceMethods
13
+ extend FavStar::ActsAsFaveable::SingletonMethods
14
+ end
15
+ end
16
+
17
+ module SingletonMethods
18
+ end
19
+
20
+ module InstanceMethods
21
+
22
+ def favs
23
+ Fave.where(:faveable_id => id, :faveable_type => self.class.name).count
24
+ end
25
+
26
+ def favers_who_faved
27
+ self.faves.map(&:faver).uniq
28
+ end
29
+
30
+ def faved_by?(faver)
31
+ 0 < Fave.where(
32
+ :faveable_id => self.id,
33
+ :faveable_type => self.class.name,
34
+ :faver_type => faver.class.name,
35
+ :faver_id => faver.id
36
+ ).count
37
+ end
38
+
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,56 @@
1
+ module FavStar #:nodoc:
2
+ module ActsAsFaver #:nodoc:
3
+
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def acts_as_faver
10
+
11
+ has_many :votes, :as => :faver, :dependent => :destroy
12
+
13
+ include FavStar::ActsAsFaver::InstanceMethods
14
+ extend FavStar::ActsAsFaver::SingletonMethods
15
+
16
+ end
17
+ end
18
+
19
+ module SingletonMethods
20
+ end
21
+
22
+ module InstanceMethods
23
+
24
+ def faved?(faveable)
25
+ 0 < Fave.where(
26
+ :faver_id => self.id,
27
+ :faver_type => self.class.name,
28
+ :faveable_id => faveable.id,
29
+ :faveable_type => faveable.class.name
30
+ ).count
31
+ end
32
+
33
+ def fave!(faveable)
34
+ Fave.create!(:faveable => faveable, :faver => self) unless self.faved?(faveable)
35
+ end
36
+
37
+ def unfave!(faveable)
38
+ Fave.where(
39
+ :faver_id => self.id,
40
+ :faver_type => self.class.name,
41
+ :faveable_id => faveable.id,
42
+ :faveable_type => faveable.class.name
43
+ ).map(&:destroy)
44
+ end
45
+
46
+ def toggle_fave!(faveable)
47
+ if self.faved?(faveable)
48
+ unfave!(faveable)
49
+ else
50
+ fave!(faveable)
51
+ end
52
+ end
53
+
54
+ end
55
+ end
56
+ end
data/lib/fav_star.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'acts_as_faveable'
2
+ require 'acts_as_faver'
3
+
4
+ ActiveRecord::Base.send(:include, FavStar::ActsAsFaveable)
5
+ ActiveRecord::Base.send(:include, FavStar::ActsAsFaver)
@@ -0,0 +1,27 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ class FavStarGenerator < Rails::Generators::Base
4
+
5
+ include Rails::Generators::Migration
6
+
7
+ source_root File.expand_path('../templates', __FILE__)
8
+
9
+ # Implement the required interface for Rails::Generators::Migration.
10
+ def self.next_migration_number(dirname) #:nodoc:
11
+ next_migration_number = current_migration_number(dirname) + 1
12
+ if ActiveRecord::Base.timestamped_migrations
13
+ [Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % next_migration_number].max
14
+ else
15
+ "%.3d" % next_migration_number
16
+ end
17
+ end
18
+
19
+ def create_migration
20
+ migration_template 'migration.rb', File.join('db', 'migrate', 'fav_star_migration.rb')
21
+ end
22
+
23
+ def move_fav_model
24
+ template 'fave.rb', File.join('app', 'models', 'fave.rb')
25
+ end
26
+
27
+ end
@@ -0,0 +1,15 @@
1
+ class Fave < ActiveRecord::Base
2
+
3
+ scope :for_faver, lambda { |*args| where(["faver_id = ? AND faver_type = ?", args.first.id, args.first.class.name]) }
4
+ scope :for_faveable, lambda { |*args| where(["faveable_id = ? AND faveable_type = ?", args.first.id, args.first.class.name]) }
5
+ scope :recent, lambda { |*args| where(["created_at > ?", (args.first || 2.weeks.ago)]) }
6
+ scope :descending, order("created_at DESC")
7
+
8
+ belongs_to :faveable, :polymorphic => true
9
+ belongs_to :faver, :polymorphic => true
10
+
11
+ attr_accessible :faver, :faveable
12
+
13
+ validates_uniqueness_of :faveable_id, :scope => [:faveable_type, :faver_type, :faver_id]
14
+
15
+ end
@@ -0,0 +1,22 @@
1
+ class FavStarMigration < ActiveRecord::Migration
2
+ def self.up
3
+
4
+ create_table :faves, :force => true do |t|
5
+ t.references :faveable, :polymorphic => true, :null => false
6
+ t.references :faver, :polymorphic => true
7
+ t.timestamps
8
+ end
9
+
10
+ add_index :faves, ["faver_id", "faver_type"], :name => "fk_favers"
11
+ add_index :faves, ["faveable_id", "faveable_type"], :name => "fk_faveables"
12
+ add_index :faves, ["faver_id", "faver_type", "faveable_id", "faveable_type"], :unique => true, :name => "only_one_fav_per_user"
13
+
14
+ end
15
+
16
+ def self.down
17
+
18
+ drop_table :faves
19
+
20
+ end
21
+
22
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fav_star
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
+ platform: ruby
11
+ authors:
12
+ - Brady Bouchard
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-08-19 00:00:00 +10:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: fav_star provides a fairly straightforward module that allows you to keep track of which models your users have favourited.
22
+ email: brady@ldawn.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README.markdown
29
+ files:
30
+ - .gitignore
31
+ - CHANGELOG.markdown
32
+ - MIT-LICENSE
33
+ - README.markdown
34
+ - Rakefile
35
+ - VERSION
36
+ - fav_star.gemspec
37
+ - lib/acts_as_faveable.rb
38
+ - lib/acts_as_faver.rb
39
+ - lib/fav_star.rb
40
+ - lib/generators/fav_star/fav_star_generator.rb
41
+ - lib/generators/fav_star/templates/fave.rb
42
+ - lib/generators/fav_star/templates/migration.rb
43
+ has_rdoc: true
44
+ homepage: http://github.com/brady8/favstar
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options:
49
+ - --charset=UTF-8
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.3.7
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Mark your ActiveRecord models as favourites with only a couple of lines of code.
75
+ test_files: []
76
+