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

Have other rights appear in groupless #5

Merged
merged 7 commits into from
Jul 6, 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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ script: "bundle exec rake spec"
gemfile:
- gemfiles/rails3.gemfile
- gemfiles/rails4.gemfile
- gemfiles/rails5.gemfile
notifications:
email:
- support@travellink.com.au
Expand Down
6 changes: 6 additions & 0 deletions gemfiles/rails5.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source 'https://rubygems.org'
gemspec :path => '../'

group :development, :test do
gem 'rails', '~> 5.0'
end
76 changes: 41 additions & 35 deletions lib/right_on/right.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,60 +23,66 @@ def associate_group(klass, group)
unless @@restricted_by_right_classes.include?(klass)
@@restricted_by_right_classes << klass
end
has_one klass.table_name.singularize.to_sym, :dependent => :restrict
has_one klass.table_name.singularize.to_sym, dependent: :restrict_with_exception
end

def rights_yaml(file_path)
@@rights_yaml = file_path
end

def by_groups
rights = []
rights += regular_rights_with_group
rights += restricted_rights_with_group
rights = regular_rights_with_group + restricted_rights_with_group
rights += (Right.all - rights)
Copy link
Contributor

@alxberardi alxberardi Jul 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm reading this wrong, but wouldn't this end up assigning all rights to the rights variable?
It reads like

a = [1,2,3]
b = [4,5,6]
all = [1,2,3,4,5,6,7,8,9]
sum = a + b
sum = sum + all - sum # => all

Or are the regular_rights_with_group and restricted_rights_with_group somehow decorated?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, I see the decoration happening below, with the assignment of the group. That answers the above question.

Copy link

@akilmadan akilmadan Jul 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional suggestion. Maybe something like...

rights_with_groups = regular_rights_with_group + restricted_rights_with_group
other_rights = Rights.all - rights_with_groups
rights = rights_with_groups + other_rights

It's an extra line but does make it easier to understand the logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alxberardi Shall I make it like the above and merge?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy how it is now. If you want to implement @vgmaster21 's suggestion feel free to. I just didn't immediately notice the assignment of the groups to the rights.

rights.group_by(&:group)
end

def regular_rights_with_group
yaml = YAML::load_file(@@rights_yaml)
rights = []
rights_by_name = Hash[Right.all.map{|r| [r.name, r]}]
yaml['rights'].each_pair do |group, right_names|
rights_for_group = []
right_names.each do |right_name|
if right_name.is_a?(String) # controller
r = rights_by_name[right_name]
raise right_name if r.nil?
rights_for_group << r
else right_name.is_a?(Hash) # controller + actions
controller, actions = right_name.first
r = rights_by_name[controller]
if r
rights_for_group << r
end
actions.each do |action|
name = "#{controller}##{action}"
r = rights_by_name[name]
raise name.inspect + "****" + right_name.inspect + '---' + action_right.inspect if r.nil?
rights_for_group << r
end
end
end
rights_for_group.each{|r| r.group = group}
rights += rights_for_group
yaml_rights.each_pair.flat_map do |group, right_names|
right_names
.flat_map { |right_name| right_name_to_rights(right_name) }
.each { |r| r.group = group }
end
rights
end

def yaml_rights
YAML::load_file(@@rights_yaml)['rights']
end

def right_name_to_rights(right_name)
case right_name
when String # controller
[rights_by_name!(right_name)]
when Hash # controller + actions
controller, actions = right_name.first
controller_rights(controller) + action_rights(controller, actions)
end
end

def controller_rights(controller)
r = rights_by_name[controller]
return [] unless r
[r]
end

def action_rights(controller, actions)
actions.map { |action| rights_by_name!("#{controller}##{action}") }
end

def rights_by_name
@rights_by_name ||= Hash[Right.all.map{|r| [r.name, r]}]
end

def rights_by_name!(name)
rights_by_name[name] or fail name.inspect
end

def restricted_rights_with_group
rights = []
@@restricted_by_right_classes.each do |klass|
@@restricted_by_right_classes.flat_map do |klass|
group = klass.restricted_by_right_group
rights += all_rights(klass).map(&:right).each do |right|
all_rights(klass).map(&:right).sort_by(&:name).each do |right|
right.group = group
end
end
rights
end

def all_rights(klass)
Expand Down
10 changes: 5 additions & 5 deletions right_on.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_dependency('activerecord', [">= 3.2.0", "< 5.0.0"])
spec.add_dependency('activesupport', [">= 3.2.0", "< 5.0.0"])
spec.add_dependency('dependent_restrict', [">= 0.2.1"])
spec.add_dependency('input_reader', ["~> 0.0"])
spec.add_development_dependency "bundler", "~> 1.3"
spec.add_dependency 'activerecord', '>= 3.2.0'
spec.add_dependency 'activesupport', '>= 3.2.0'
spec.add_dependency 'dependent_restrict', '>= 0.2.3'
spec.add_dependency 'input_reader', '~> 0.0'
spec.add_development_dependency 'bundler', '~> 1.3'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rspec'
spec.add_development_dependency 'simplecov'
Expand Down