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

Modulise #2

Merged
merged 4 commits into from
Jun 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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