Skip to content
This repository has been archived by the owner on Jan 4, 2021. It is now read-only.

Commit

Permalink
Merge 5779e75 into d52ff8c
Browse files Browse the repository at this point in the history
  • Loading branch information
alxberardi committed Jun 3, 2016
2 parents d52ff8c + 5779e75 commit 38285c0
Show file tree
Hide file tree
Showing 10 changed files with 336 additions and 329 deletions.
23 changes: 11 additions & 12 deletions lib/right_on/generators/templates/right_migration.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
class Right < ActiveRecord::Base
has_and_belongs_to_many :roles
class Add<%= parsed_right_name.camelize %>Right < ActiveRecord::Migration
validates_presence_of :name
validates_uniqueness_of :name
end
class Right < ActiveRecord::Base
has_and_belongs_to_many :roles
class Role < ActiveRecord::Base
has_and_belongs_to_many :rights
validates_presence_of :name
validates_uniqueness_of :name
end
validates_presence_of :title
validates_uniqueness_of :title
end
class Role < ActiveRecord::Base
has_and_belongs_to_many :rights
validates_presence_of :title
validates_uniqueness_of :title
end
class Add<%= parsed_right_name.camelize %>Right < ActiveRecord::Migration
def self.up
right_for_roles = Right.find_by_name("<%= right_for_roles %>")
Right.create(
Expand Down
71 changes: 36 additions & 35 deletions lib/right_on/restricted_by_right.rb
Original file line number Diff line number Diff line change
@@ -1,52 +1,53 @@
module RestrictedByRight
module RightOn
module RestrictedByRight

def self.included(base)
base.extend(ClassMethods)
end
def self.included(base)
base.extend(ClassMethods)
end

module ClassMethods
def restricted_by_right(options = {})
options ||= {}
options[:group] ||= 'other'
Right.associate_group(self, options[:group])
module ClassMethods
def restricted_by_right(options = {})
options ||= {}
options[:group] ||= 'other'
Right.associate_group(self, options[:group])

class << self
def accessible_to(user)
all.select{|o| user.rights.include?(o.right)}
class << self
def accessible_to(user)
all.select{|o| user.rights.include?(o.right)}
end
end
end

include InstanceMethods
include InstanceMethods

belongs_to :right, :class_name => 'RightOn::Right'
before_create :create_access_right!
after_destroy :destroy_access_right!
end

belongs_to :right
before_create :create_access_right!
after_destroy :destroy_access_right!
end

end
module InstanceMethods

module InstanceMethods
private

private
def create_access_right!
right_name = "#{self.class.name.titleize}: #{name}"
self.right = find_right(right_name) || Right.create!(:name => right_name)
end

def create_access_right!
right_name = "#{self.class.name.titleize}: #{name}"
self.right = find_right(right_name) || Right.create!(:name => right_name)
end
def find_right(name)
if Right.respond_to? :find_by
Right.find_by(:name => name)
else
Right.find_by_name(name)
end
end

def find_right(name)
if Right.respond_to? :find_by
Right.find_by(:name => name)
else
Right.find_by_name(name)
def destroy_access_right!
self.right.try(:destroy)
end
end

def destroy_access_right!
self.right.try(:destroy)
end
end

end

end

Loading

0 comments on commit 38285c0

Please sign in to comment.