├── .gitignore ├── init.rb ├── lib ├── acts_as_favable.rb ├── generators │ └── favorite │ │ ├── USAGE │ │ ├── templates │ │ ├── favorite.rb │ │ └── create_favorites.rb │ │ └── favorite_generator.rb ├── favorite_methods.rb └── favable_methods.rb ├── install.rb ├── test ├── test_helper.rb └── acts_as_favable_test.rb ├── Gemfile ├── Rakefile ├── MIT-LICENSE ├── README.markdown └── acts_as_favable.gemspec /.gitignore: -------------------------------------------------------------------------------- 1 | .svn 2 | -------------------------------------------------------------------------------- /init.rb: -------------------------------------------------------------------------------- 1 | require File.join(File.dirname(__FILE__), 'lib', 'acts_as_favable') -------------------------------------------------------------------------------- /lib/acts_as_favable.rb: -------------------------------------------------------------------------------- 1 | require 'favable_methods' 2 | require 'favorite_methods' -------------------------------------------------------------------------------- /install.rb: -------------------------------------------------------------------------------- 1 | puts "To create the favorite model please run:" 2 | puts "rails generate favorite" 3 | -------------------------------------------------------------------------------- /test/test_helper.rb: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'test/unit' 3 | require 'active_support' 4 | -------------------------------------------------------------------------------- /lib/generators/favorite/USAGE: -------------------------------------------------------------------------------- 1 | Description: 2 | Copies favorite.rb to app/models/. 3 | Copies create_favorites.rb to db/migrate 4 | 5 | Examples: 6 | `rails generate favorite` 7 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in acts_as_favable.gemspec 4 | gemspec 5 | 6 | group :development, :test do 7 | gem 'minitest' 8 | gem 'sqlite3', '~> 1.4.0' 9 | end -------------------------------------------------------------------------------- /test/acts_as_favable_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class ActsAsFavableTest < ActiveSupport::TestCase 4 | # Replace this with your real tests. 5 | test "the truth" do 6 | assert true 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'bundler/gem_tasks' 2 | require 'rake/testtask' 3 | 4 | Rake::TestTask.new(:test) do |t| 5 | t.libs << 'test' 6 | t.pattern = 'test/**/*_test.rb' 7 | t.verbose = true 8 | end 9 | 10 | task default: :test 11 | 12 | -------------------------------------------------------------------------------- /lib/generators/favorite/templates/favorite.rb: -------------------------------------------------------------------------------- 1 | class Favorite < ApplicationRecord 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 12 | -------------------------------------------------------------------------------- /lib/generators/favorite/templates/create_favorites.rb: -------------------------------------------------------------------------------- 1 | class CreateFavorites < ActiveRecord::Migration[8.0] 2 | def change 3 | create_table :favorites do |t| 4 | t.string :note, limit: 50, default: "" 5 | t.references :favable, polymorphic: true, index: true 6 | t.references :user, index: true 7 | t.timestamps 8 | end 9 | end 10 | end -------------------------------------------------------------------------------- /lib/generators/favorite/favorite_generator.rb: -------------------------------------------------------------------------------- 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 19 | -------------------------------------------------------------------------------- /lib/favorite_methods.rb: -------------------------------------------------------------------------------- 1 | module ActsAsFavable 2 | module Favorite 3 | 4 | def self.included(favorite_model) 5 | favorite_model.extend Finders 6 | favorite_model.scope :in_order, -> { order('created_at ASC') } 7 | favorite_model.scope :recent, -> { 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 25 | -------------------------------------------------------------------------------- /MIT-LICENSE: -------------------------------------------------------------------------------- 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. 21 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 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.. 48 | -------------------------------------------------------------------------------- /lib/favable_methods.rb: -------------------------------------------------------------------------------- 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, **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 = self.name 21 | Favorite.find_favorites_for_favable(favable, obj.id) 22 | end 23 | 24 | def find_favorites_by_user(user) 25 | favable = self.name 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) 45 | -------------------------------------------------------------------------------- /acts_as_favable.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | 3 | Gem::Specification.new do |s| 4 | s.name = 'acts_as_favable' 5 | s.version = "2.0.0" 6 | 7 | s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= 8 | s.authors = ["Andy Wang"] 9 | s.date = Time.now.strftime('%Y-%m-%d') 10 | s.description = 'Plugin/Gem that provides favorites functionality' 11 | s.email = 'wangyaodi@gmail.com' 12 | s.extra_rdoc_files = ["README.markdown", "MIT-LICENSE"] 13 | s.files = ["MIT-LICENSE", 14 | "README.markdown", 15 | "lib/acts_as_favable.rb", 16 | "lib/favorite_methods.rb", 17 | "lib/favable_methods.rb", 18 | "lib/generators", 19 | "lib/generators/favorite", 20 | "lib/generators/favorite/favorite_generator.rb", 21 | "lib/generators/favorite/templates", 22 | "lib/generators/favorite/templates/favorite.rb", 23 | "lib/generators/favorite/templates/create_favorites.rb", 24 | "init.rb", 25 | "install.rb"] 26 | s.homepage = 'https://github.com/yorzi/acts_as_favable' 27 | s.require_paths = ["lib"] 28 | s.summary = 'Plugin/gem that provides favorite functionality' 29 | s.required_ruby_version = '>= 2.6.0' 30 | 31 | s.add_dependency 'activerecord', '>= 6.0', '< 9.0' 32 | s.add_dependency 'activesupport', '>= 6.0', '< 9.0' 33 | 34 | s.add_development_dependency 'rake' 35 | s.add_development_dependency 'rails', '>= 6.0', '< 9.0' 36 | end 37 | --------------------------------------------------------------------------------