Sha256: 160ec4608afb5915832e1bb4b60d7c116f81e59250af3184b86a6caf4242ff97

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

say "Building roles"
generate(:model, "role name:string")
generate(:model, "user_role user_id:integer role_id:integer")

inject_into_file 'app/models/user.rb', :after => "class User < ActiveRecord::Base\n" do
<<-RUBY

  # roles
  has_many :user_roles
  has_many :roles, :through => :user_roles

RUBY
end

inject_into_file 'app/models/user_role.rb', :after => "class UserRole < ActiveRecord::Base\n" do
<<-RUBY
  belongs_to :user
  belongs_to :role
RUBY
end

inject_into_file 'app/models/role.rb', :after => "class Role < ActiveRecord::Base\n" do
<<-RUBY

  # roles
  has_many :user_roles
  has_many :users, :through => :user_roles

  def self.sanitize role
    role.to_s.humanize.split(' ').each{ |word| word.capitalize! }.join(' ')
  end
  
RUBY
end

create_file 'app/models/ability.rb' do
<<-RUBY
class Ability
  include CanCan::Ability
 
  def initialize(user)
    user ||= User.new # guest user
 
    if user.role? :admin
      can :manage, :all
    end
  end
end
RUBY
end

inject_into_file 'app/models/user.rb', :before => "end\n" do
<<-RUBY

  def is?(role)
    return !!self.roles.find_by_name( Role.sanitize role )
  end

RUBY
end

inject_into_file 'app/controllers/application_controller.rb', :before => "end\n" do
<<-RUBY

  rescue_from CanCan::AccessDenied do |exception|
    flash[:error] = "Access Denied"
    redirect_to root_url
  end
RUBY
end


append_file 'db/seeds.rb' do
<<-FILE
# create an example admin.
admin = Role.create! :name => 'Admin'
user = User.create(email: 'admin@example.org', password: 'password!', password_confirmation: 'password!')
user.roles << admin
user.save

FILE
end





Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shinkansen-0.1 templates/default/lib/cancan.rb