Skip to content

Commit

Permalink
permits factory works?
Browse files Browse the repository at this point in the history
  • Loading branch information
kristianmandrup committed Dec 23, 2011
1 parent 2f8bc18 commit d7ab263
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 25 deletions.
42 changes: 31 additions & 11 deletions lib/cantango/permits_ext/builder/permit/base.rb
Expand Up @@ -5,22 +5,22 @@ class CreatePermitError < StandardError; end

include CanTango::Helpers::Debug

attr_accessor :ability, :finder
attr_accessor :ability, :options

# creates the factory for the ability
# note that the ability contains the roles and role groups of the user (or account)
# @param [Permits::Ability] the ability
def initialize ability, finder
@ability = ability
@finder = finder
def initialize ability, options = {}
@ability, @options = [ability, options]
end

def self.permit_type
self.name.demodulize.gsub(/Permit/, '').gsub(/(.*)(Builder)/, '\1').underscore.to_sym
module ClassMethods
include CanTango::Permit::Helper::Naming
end
extend ClassMethods

def permit_type
self.class.permit_type
self.class.permit_name
end

# should be implemented by builder subclass!
Expand All @@ -34,17 +34,37 @@ def build
# If no permit Class can be found, it should return nil
# @param [Symbol] the name
# @return the permit Class or nil if not found
def create_permit name
def create_permits
begin
permit_class(name).new ability
names.inject([]) do |res, name|
permit = permit(name)
res << permit unless permit.disabled?
end
rescue RuntimeError => e
# puts "Error instantiating Permit instance for #{name}, cause: #{e}" if CanTango.debug?
debug "Error instantiating Permit instance for #{name}, cause: #{e}"
nil
end
end

def permit name
clazz = permit_class(name)
permit = clazz.new ability if clazz
end

def names
CanTango.config.permits.registered_for(permit_type).registered_names
end

def finder name
CanTango::Finder::Permit::Base.new name, options.merge(:type => permit_type)
end

def permit_class name
finder.find_permit
finder(name).find_permit
end

def not_building
"Not building any #{permit_type.to_s.humanize} permit"
end

# delegate to ability
Expand Down
14 changes: 9 additions & 5 deletions lib/cantango/permits_ext/builder/permit/user_type.rb
Expand Up @@ -3,21 +3,25 @@ module Builder::Permit
class UserType < Base
# class NoAvailableRoles < StandardError; end

def initialize ability, options = {}
super
end

# builds a list of Permits for each role of the current ability user (or account)
# @return [Array<Permit::Base>] the role permits built for this ability
def build
puts debug_msg if CanTango.debug?
[permit].compact
debug debug_msg
permits
end

protected

def debug_msg
permit ? "Building UserPermit for #{user}, permit: #{permit}" : "Not building any UserPermit"
permits ? "Building UserPermit permits" : not_building
end

def permit
@permit ||= create_permit permit_type
def permits
@permit ||= create_permits
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/cantango/permits_ext/engine/permits.rb
Expand Up @@ -32,7 +32,7 @@ def valid?
# has a role or a role group
# also execute any permit marked as special
def permits_of type
@permits ||= permit_factory(type).build!
@permits ||= permit_factory(type).create
end

def permit_class_names
Expand Down Expand Up @@ -68,7 +68,7 @@ def key_method_names
end

def available_permits_for type
CanTango.config.permits.available_permits[type]
CanTango.config.permits.types.available
end
end
end
Expand Down
8 changes: 3 additions & 5 deletions lib/cantango/permits_ext/factory/permits.rb
Expand Up @@ -18,13 +18,9 @@ def create
end

def permits
permits_builder.new ability, permit_finder
permits_builder.new ability
end

def permit_finder
CanTango::Finder::Permit::Base.new name, options
end

def permits_builder
permits_builder_class.constantize
end
Expand All @@ -34,6 +30,8 @@ def permits_builder_class
"CanTango::Builder::Permit::#{type.to_s.camelize}"
end

protected

def enabled_permit_types
CanTango.config.permits.types.enabled
end
Expand Down
4 changes: 2 additions & 2 deletions spec/cantango/factory/permits_spec.rb
@@ -1,7 +1,6 @@
require 'spec_helper'
require 'fixtures/models'

CanTango.config.permits.types.register :user_type
CanTango.config.debug.set :on

class AdminPermit < CanTango::Permit::UserType
Expand All @@ -21,9 +20,10 @@ def calc_rules
@ua = UserAccount.new @user
@user.account = @ua
@ability = CanTango::Ability::Base.new @user
@factory = CanTango::Factory::Permits.new @ability, :user_type
end

subject { CanTango::Factory::Permits.new @ability, :user_type }
subject { @factory }

describe 'attributes' do
it "should have an ability" do
Expand Down

0 comments on commit d7ab263

Please sign in to comment.