acts_as_favable 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [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/README.markdown ADDED
@@ -0,0 +1,47 @@
1
+ Acts As Favable
2
+ =================
3
+
4
+ Allows for favorite refer to be added to multiple and different models.
5
+
6
+ ## Resources
7
+
8
+ ####Install(on Rails3)
9
+
10
+ To install as a plugin
11
+
12
+ rails plugin install http://github.com/yorzi/acts_as_favable.git
13
+
14
+ To install as a gem
15
+
16
+ sudo gem install acts_as_favable
17
+
18
+ Generate your favorite model:
19
+
20
+ rails generate favorite
21
+
22
+ Then migrate your database:
23
+
24
+ rake db:migrate
25
+
26
+ ## Usage
27
+ #### Make your ActiveRecord model act as favable.
28
+
29
+ class Model < ActiveRecord::Base
30
+ acts_as_favable
31
+ end
32
+
33
+ ####mark a favorite flag to a model instance
34
+
35
+ favable = Model.create
36
+ favable.favorites.create(:note => "I like this and will make it one of my favorites")
37
+
38
+ ####Fetch favorites for a favable model:
39
+
40
+ favable = Model.find(1)
41
+ favorites = favable.favorites.recent.limit(10).all
42
+
43
+ Attention: This plugin/gem is heavily influenced by Acts As Taggable and almost copied from acts_at_commentable.
44
+
45
+ ## More
46
+
47
+ Blog post..
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), 'rails', 'init')
data/install.rb ADDED
@@ -0,0 +1,2 @@
1
+ puts "To create the favorite model please run:"
2
+ puts "rails generate favorite"
@@ -0,0 +1,2 @@
1
+ require 'favable_methods'
2
+ require 'favorite_methods'
@@ -0,0 +1,44 @@
1
+ require 'active_record'
2
+
3
+ module Acts #:nodoc:
4
+ module Favable #:nodoc:
5
+
6
+ def self.included(base)
7
+ base.extend ClassMethods
8
+ end
9
+
10
+ module ClassMethods
11
+ def acts_as_favable(options={})
12
+ has_many :favorites, {:as => :favable, :dependent => :destroy}.merge(options)
13
+ include Acts::Favable::InstanceMethods
14
+ extend Acts::Favable::SingletonMethods
15
+ end
16
+ end
17
+
18
+ module SingletonMethods
19
+ def find_favorites_for(obj)
20
+ favable = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s
21
+ Favorite.find_favorites_for_favable(favable, obj.id)
22
+ end
23
+
24
+ def find_favorites_by_user(user)
25
+ favable = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s
26
+ Favorite.where(["user_id = ? and favable_type = ?", user.id, favable]).order("created_at DESC")
27
+ end
28
+ end
29
+
30
+ # This module contains instance methods
31
+ module InstanceMethods
32
+ def favorites_ordered_by_submitted
33
+ Favorite.find_favorites_for_favable(self.class.name, id)
34
+ end
35
+
36
+ def add_favorite(favorite)
37
+ favorites << favorite
38
+ end
39
+ end
40
+
41
+ end
42
+ end
43
+
44
+ ActiveRecord::Base.send(:include, Acts::Favable)
@@ -0,0 +1,24 @@
1
+ module ActsAsFavable
2
+ module Favorite
3
+
4
+ def self.included(favorite_model)
5
+ favorite_model.extend Finders
6
+ favorite_model.scope :in_order, favorite_model.order('created_at ASC')
7
+ favorite_model.scope :recent, favorite_model.order('created_at DESC')
8
+ end
9
+
10
+ module Finders
11
+ def find_favorites_by_user(user)
12
+ where(["user_id = ?", user.id]).order("created_at DESC")
13
+ end
14
+
15
+ def find_favorites_for_favable(favable_str, favable_id)
16
+ where(["favable_type = ? and favable_id = ?", favable_str, favable_id]).order("created_at DESC")
17
+ end
18
+
19
+ def find_favable(favable_str, favable_id)
20
+ favable_str.constantize.find(favable_id)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,18 @@
1
+ require 'rails/generators/migration'
2
+
3
+ class FavoriteGenerator < Rails::Generators::Base
4
+ include Rails::Generators::Migration
5
+
6
+ def self.source_root
7
+ @_acts_as_favable_source_root ||= File.expand_path("../templates", __FILE__)
8
+ end
9
+
10
+ def self.next_migration_number(path)
11
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
12
+ end
13
+
14
+ def create_model_file
15
+ template "favorite.rb", "app/models/favorite.rb"
16
+ migration_template "create_favorites.rb", "db/migrate/create_favorites.rb"
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ class CreateFavorites < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :favorites do |t|
4
+ t.string :note, :limit => 50, :default => ""
5
+ t.references :favable, :polymorphic => true
6
+ t.references :user
7
+ t.timestamps
8
+ end
9
+
10
+ add_index :favorites, :favable_type
11
+ add_index :favorites, :favable_id
12
+ add_index :favorites, :user_id
13
+ end
14
+
15
+ def self.down
16
+ drop_table :favorites
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ class Favorite < ActiveRecord::Base
2
+
3
+ include ActsAsFavable::Favorite
4
+
5
+ belongs_to :favable, :polymorphic => true
6
+
7
+ default_scope :order => 'created_at ASC'
8
+
9
+ # NOTE: Favorite belongs to a user
10
+ belongs_to :user
11
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_favable
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Andy Wang
14
+ autorequire: acts_as_favable
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-29 00:00:00 +08:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Plugin/Gem that provides favorites functionality
23
+ email: wangyaodi@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README.markdown
30
+ - MIT-LICENSE
31
+ files:
32
+ - MIT-LICENSE
33
+ - README.markdown
34
+ - lib/acts_as_favable.rb
35
+ - lib/favorite_methods.rb
36
+ - lib/favable_methods.rb
37
+ - lib/generators/favorite/favorite_generator.rb
38
+ - lib/generators/favorite/templates/favorite.rb
39
+ - lib/generators/favorite/templates/create_favorites.rb
40
+ - init.rb
41
+ - install.rb
42
+ has_rdoc: true
43
+ homepage: https://github.com/yorzi/acts_as_favable
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options: []
48
+
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
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
+ hash: 3
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.3.7
73
+ signing_key:
74
+ specification_version: 1
75
+ summary: Plugin/gem that provides favorite functionality
76
+ test_files: []
77
+