add generator

This commit is contained in:
ZengTao 2016-06-20 15:57:31 +08:00
parent 16fc627d62
commit 5ac942ec97
6 changed files with 72 additions and 4 deletions

View File

@ -9,9 +9,9 @@ Gem::Specification.new do |spec|
spec.authors = ["ZengTao"]
spec.email = ["so.zengtao@gmail.com"]
spec.summary = %q{TODO: Write a short summary, because Rubygems requires one.}
spec.description = %q{TODO: Write a longer description or delete this line.}
spec.homepage = "TODO: Put your gem's website or public repo URL here."
spec.summary = %q{ActsAsFollowable Just Save Time For You.}
spec.description = %q{ActsAsFollowable Just Save Time For You.}
spec.homepage = "https://github.com/w-zengtao"
spec.license = "MIT"
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
@ -30,4 +30,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "bundler", "~> 1.12"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "rails", "~> 4.0"
end

View File

@ -1,5 +1,5 @@
require "acts_as_followable/version"
module ActsAsFollowable
# Your code goes here...
autoload :
end

View File

@ -0,0 +1,19 @@
module ActsAsFollowable
module Followable
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def acts_as_followable #This means can be followed - 被关注
include ActsAsFollowable::Followable::InstanceMethods
end
end
module InstanceMethods
# 1: 查看某个 模型 关注我的所有对象
# 2: 查看某个 模型 的某个 实例 是否关注我了
end
end
end

View File

@ -0,0 +1,22 @@
require 'rails/generators'
require 'rails/generators/migration'
class ActsAsFollowableGenerator < Rails::Generators::Base
include Rails::Generators::Migration
def self.source_root
@source_root ||= File.join(File.dirname(__FILE__), 'templates')
end
def self.next_migration_number(dirname)
Time.now.utc.strftime("%Y%m%d%H%M%S")
end
def create_migration_file
migration_template 'migration.rb', 'db/migrate/acts_as_followable_migration.rb'
end
def create_model
template "follow.rb", File.join('app/models', "follow.rb")
end
end

View File

@ -0,0 +1,12 @@
class Follow < ActiveRecord::Base
# extend ActsAsFollower::FollowerLib
# extend ActsAsFollower::FollowScopes
belongs_to :followable, :polymorphic => true
belongs_to :follower, :polymorphic => true
def block!
self.update_attribute(:blocked, true)
end
end

View File

@ -0,0 +1,14 @@
class ActsAsFollowableMigration < ActiveRecord::Migration[5.0]
def self.up
create_table :follows, force: true do |t|
t.references :followable, polymorphic: true, null: false
t.references :follower, polymorphic: true, null: false
t.boolean :blocked, default: false, null: false
t.timestamps
end
end
def self.down
drop_table :follows
end
end