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

Commit

Permalink
Extract rights_yaml to where it is used (in ByGroups)
Browse files Browse the repository at this point in the history
  • Loading branch information
mnoack committed Apr 6, 2018
1 parent a9a15e1 commit ba677e1
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This changelog adheres to [Keep a CHANGELOG](http://keepachangelog.com/).

### Improved
- Improve tests for RightOn::ByGroup
- Internal improvement of RightOn::ByGroup

### Fixed
- [TT-3352] Ensure roles currently in use cannot be deleted
Expand Down
8 changes: 8 additions & 0 deletions lib/right_on.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@ module RightOn
require 'rails'
require 'right_on/railtie'
require 'right_on/rights_manager'

def self.rights_yaml(file_path = nil)
if file_path
@rights_yaml = file_path
else
@rights_yaml
end
end
end
19 changes: 10 additions & 9 deletions lib/right_on/by_group.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
module RightOn
class ByGroup
def self.rights
new.by_groups
end

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

def by_groups
rights = regular_rights_with_group
rights += (Right.all - rights)
rights.group_by(&:group)
yaml_rights.each_pair.with_object({}) do |(group, right_names), hash|
hash[group] = right_names
.flat_map { |right_name| right_name_to_rights(right_name) }
end
end

private

def regular_rights_with_group
RightOn::Right.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
def yaml_rights
YAML::load_file(RightOn.rights_yaml)['rights']
end

def right_name_to_rights(right_name)
Expand Down
16 changes: 0 additions & 16 deletions lib/right_on/right.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,6 @@ class Right < ActiveRecord::Base
after_save :clear_cache
after_destroy :clear_cache

attr_accessor :group

class << self
def rights_yaml(file_path)
@@rights_yaml = file_path
end

def by_groups
RightOn::ByGroup.new.by_groups
end

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

# Is this right allowed for the given context?
#
# Context params is an option hash:
Expand Down
4 changes: 2 additions & 2 deletions spec/by_group_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
require 'spec_helper'

RightOn::Right.rights_yaml 'db/rights_roles.yml'
RightOn.rights_yaml 'db/rights_roles.yml'

describe RightOn::ByGroup do
let(:rights) { Bootstrap.various_rights_with_actions }

it 'should identify correct groups' do
rights # load rights
expect(RightOn::Right.by_groups).to eq(
expect(RightOn::ByGroup.rights).to eq(
'general' => [
rights[:models],
rights[:models_index],
Expand Down
2 changes: 1 addition & 1 deletion spec/support/coverage_loader.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'simplecov-rcov'
require 'coveralls'
require 'coverage/kit'
Coverage::Kit.setup(minimum_coverage: 91.7)
Coverage::Kit.setup(minimum_coverage: 91.3)

0 comments on commit ba677e1

Please sign in to comment.