Skip to content
This repository has been archived by the owner on Aug 23, 2022. It is now read-only.

Commit

Permalink
Rubocop compliant code
Browse files Browse the repository at this point in the history
  • Loading branch information
xjunior committed Nov 25, 2019
1 parent e571c4f commit 2332e53
Show file tree
Hide file tree
Showing 31 changed files with 178 additions and 48 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
inherit_from: .rubocop_todo.yml
31 changes: 31 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2019-11-20 02:06:29 -0300 using RuboCop version 0.65.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 5
# Configuration parameters: CountComments, ExcludedMethods.
# ExcludedMethods: refine
Metrics/BlockLength:
Exclude:
- 'spec/**/*'
- 'lib/consent/rspec.rb'

# Offense count: 9
Style/Documentation:
Exclude:
- 'spec/**/*'
- 'test/**/*'
- 'lib/consent.rb'
- 'lib/consent/rspec.rb'
- 'lib/consent/ability.rb'
- 'lib/consent/action.rb'
- 'lib/consent/dsl.rb'
- 'lib/consent/permission.rb'
- 'lib/consent/permissions.rb'
- 'lib/consent/subject.rb'
- 'lib/consent/view.rb'
- 'lib/generators/consent/permissions_generator.rb'
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

# Specify your gem's dependencies in consent.gemspec
Expand Down
8 changes: 5 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
task default: :spec
7 changes: 4 additions & 3 deletions bin/console
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "bundler/setup"
require "consent"
require 'bundler/setup'
require 'consent'

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
Expand All @@ -10,5 +11,5 @@ require "consent"
# require "pry"
# Pry.start

require "irb"
require 'irb'
IRB.start
8 changes: 5 additions & 3 deletions consent.gemspec
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
# frozen_string_literal: true

lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'consent/version'

Expand All @@ -18,8 +19,9 @@ Gem::Specification.new do |spec|
spec.require_paths = ['lib']

spec.add_development_dependency 'activesupport', '>= 4.1.11'
spec.add_development_dependency 'cancancan', '~> 1.15.0'
spec.add_development_dependency 'bundler', '>= 1.17.3'
spec.add_development_dependency 'cancancan', '~> 1.15.0'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'rubocop', '~> 0.65.0'
end
6 changes: 4 additions & 2 deletions lib/consent.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'consent/version'
require 'consent/subject'
require 'consent/view'
Expand All @@ -9,7 +11,7 @@
require 'consent/railtie' if defined?(Rails)

module Consent
FULL_ACCESS = %w(1 true).freeze
FULL_ACCESS = %w[1 true].freeze

def self.default_views
@default_views ||= {}
Expand All @@ -35,7 +37,7 @@ def self.find_action(subject_key, action_key)

def self.find_view(subject_key, view_key)
views = Consent.find_subjects(subject_key)
.map{|subject| subject.views}
.map(&:views)
.reduce({}, &:merge)
views[view_key]
end
Expand Down
6 changes: 4 additions & 2 deletions lib/consent/ability.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# frozen_string_literal: true

module Consent
class Ability
include CanCan::Ability

def initialize(permissions, *args)
Consent.permissions(permissions).each do |permission|
conditions = permission.conditions(*args)
object_conditions = permission.object_conditions(*args)
can permission.action_key, permission.subject_key, conditions, &object_conditions
ocond = permission.object_conditions(*args)
can permission.action_key, permission.subject_key, conditions, &ocond
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/consent/action.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Consent
class Action
attr_reader :key, :label, :options
Expand Down
6 changes: 4 additions & 2 deletions lib/consent/dsl.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Consent
class DSL
attr_reader :subject
Expand All @@ -11,13 +13,13 @@ def with_defaults(new_defaults, &block)
DSL.build(@subject, @defaults.merge(new_defaults), &block)
end

# rubocop:disable Link/UnusedBlockArgument, Link/Eval
# rubocop:disable Lint/UnusedBlockArgument, Security/Eval
def eval_view(key, label, collection_conditions)
view key, label do |user|
eval(collection_conditions)
end
end
# rubocop:enable Link/UnusedBlockArgument, Link/Eval
# rubocop:enable Lint/UnusedBlockArgument, Security/Eval

def view(key, label, instance = nil, collection = nil, &block)
collection ||= block
Expand Down
6 changes: 6 additions & 0 deletions lib/consent/permission.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Consent
class Permission
def initialize(subject, action, view = nil)
Expand All @@ -14,6 +16,9 @@ def action_key
@action.key
end

# Disables Sytle/SafeNavigation to keep this code
# compatible with ruby < 2.3
# rubocop:disable Style/SafeNavigation
def view_key
@view && @view.key
end
Expand All @@ -25,5 +30,6 @@ def conditions(*args)
def object_conditions(*args)
@view && @view.object_conditions(*args)
end
# rubocop:enable Style/SafeNavigation
end
end
4 changes: 4 additions & 0 deletions lib/consent/permissions.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Consent
class Permissions
include Enumerable
Expand Down Expand Up @@ -25,12 +27,14 @@ def map_permission(subject, action)

def full(subject, action, view_key)
return unless Consent::FULL_ACCESS.include?(view_key.to_s.strip)

Permission.new(subject, action)
end

def partial(subject, action, view_key)
view = subject.view_for(action, view_key.to_s.to_sym)
return if view.nil?

Permission.new(subject, action, view)
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/consent/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require "consent/reloader"
# frozen_string_literal: true

require 'consent/reloader'

module Consent
# Plugs consent permission load to the Rails class loading cycle
Expand Down
2 changes: 2 additions & 0 deletions lib/consent/reloader.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Consent
# Rails file reloader to detect permission changes and apply them to consent
class Reloader
Expand Down
49 changes: 37 additions & 12 deletions lib/consent/rspec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'consent'

module Consent
Expand All @@ -11,17 +13,25 @@ module Rspec

match do |subject_key|
action = Consent.find_action(subject_key, action_key)
action && @views ? action.view_keys.sort.eql?(@views.sort) : !action.nil?
if action && @views
values_match?(action.view_keys.sort, @views.sort)
else
!action.nil?
end
end

failure_message do |subject_key|
action = Consent.find_action(subject_key, action_key)
message = "expected %s (%s) to provide action %s" % [
subject_key.to_s, subject.class, action_key
]
message = format(
'expected %<skey>s (%<sclass>s) to provide action %<action>s',
skey: subject_key.to_s, sclass: subject.class, action: action_key
)

if action && @views
'%s with views %s, but actual views are %p' % [message, @views, action.view_keys]
format(
'%<message>s with views %<views>s, but actual views are %<keys>p',
message: message, views: @views, keys: action.view_keys
)
else
message
end
Expand All @@ -35,21 +45,36 @@ module Rspec

match do |subject_key|
view = Consent.find_view(subject_key, view_key)
conditions ? view.try(:conditions, *@context).eql?(conditions) : !view.nil?
if conditions
view&.conditions(*@context).eql?(conditions)
else
!view.nil?
end
end

failure_message do |subject_key|
view = Consent.find_view(subject_key, view_key)
message = "expected %s (%s) to provide view %s with %p, but" % [
subject_key.to_s, subject.class, view_key, conditions
]
message = format(
'expected %<skey>s (%<sclass>s) to provide view %<view>s with` \
`%<conditions>p, but',
skey: subject_key.to_s, sclass: subject.class,
view: view_key, conditions: conditions
)

if view && conditions
actual_conditions = view.conditions(*@context)
'%s conditions are %p' % [message, actual_conditions]
format(
'%<message>s conditions are %<conditions>p',
message: message, conditions: actual_conditions
)
else
actual_views = Consent.find_subjects(subject_key).map(&:views).map(&:keys).flatten
'%s available views are %p' % [message, actual_views]
actual_views = Consent.find_subjects(subject_key)
.map(&:views)
.map(&:keys).flatten
format(
'%<message>s available views are %<views>p',
message: message, views: actual_views
)
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/consent/subject.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Consent
class Subject
attr_reader :key, :label, :actions, :views
Expand Down
4 changes: 3 additions & 1 deletion lib/consent/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Consent
VERSION = '0.4.3'.freeze
VERSION = '0.4.3'
end
4 changes: 4 additions & 0 deletions lib/consent/view.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Consent
class View
attr_reader :key, :label
Expand All @@ -11,11 +13,13 @@ def initialize(key, label, instance = nil, collection = nil)

def conditions(*args)
return @collection unless @collection.respond_to?(:call)

@collection.call(*args)
end

def object_conditions(*args)
return @instance unless @instance.respond_to?(:curry)

@instance.curry[*args]
end
end
Expand Down
17 changes: 12 additions & 5 deletions lib/generators/consent/permissions_generator.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
# frozen_string_literal: true

module Consent
class PermissionsGenerator < Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)
source_root File.expand_path('templates', __dir__)
argument :description, type: :string, required: false

def create_permissions
template "permissions.rb.erb", "app/permissions/#{file_path}.rb", assigns: {
description: description
}
template(
'permissions.rb.erb',
"app/permissions/#{file_path}.rb",
assigns: { description: description }
)

template "permissions_spec.rb.erb", "spec/permissions/#{file_path}_spec.rb"
template(
'permissions_spec.rb.erb',
"spec/permissions/#{file_path}_spec.rb"
)
end
end
end
8 changes: 6 additions & 2 deletions spec/nitro/consent/ability_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Consent::Ability do
Expand Down Expand Up @@ -46,9 +48,11 @@
end

it 'cannot perform action when instance condition forbids' do
past = SomeModel.new(nil, Date.new - 10)
future = SomeModel.new(nil, Date.new + 10)
permissions[:some_model] = { destroy: :future }

expect(ability).to_not be_able_to(:destroy, SomeModel.new(nil, Date.new - 10))
expect(ability).to be_able_to(:destroy, SomeModel.new(nil, Date.new + 10))
expect(ability).to_not be_able_to(:destroy, past)
expect(ability).to be_able_to(:destroy, future)
end
end
2 changes: 2 additions & 0 deletions spec/nitro/consent/action_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Consent::Action do
Expand Down
4 changes: 3 additions & 1 deletion spec/nitro/consent/dsl_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Consent::DSL do
Expand Down Expand Up @@ -101,7 +103,7 @@
it 'creates a new DSL context with merged defaults' do
defaults[:foo] = 'bar'

block = -> (_, __) {}
block = ->(_, __) {}
expected_defaults = { lol: 'rofl', foo: 'bar' }
expect(Consent::DSL).to receive(:build)
.with(subject, expected_defaults, &block)
Expand Down
Loading

0 comments on commit 2332e53

Please sign in to comment.