lib/permissive/has_permissions.rb in permissive-0.2.2.alpha vs lib/permissive/has_permissions.rb in permissive-0.2.4.alpha

- old
+ new

@@ -20,10 +20,11 @@ has_many :permissions, :class_name => 'Permissive::Permission', :as => :permitted_object do def can!(*args) options = args.extract_options! options.assert_valid_keys(:on, :reset) + options.delete(:on) if options[:on] == :global permission_matcher = case options[:on] when ActiveRecord::Base permission = proxy_owner.permissions.find_or_initialize_by_scoped_object_id_and_scoped_object_type(options[:on].id, options[:on].class.name) when Class permission = proxy_owner.permissions.find_or_initialize_by_scoped_object_id_and_scoped_object_type(nil, options[:on].name) @@ -39,12 +40,18 @@ bits_for(options[:on], args).each do |bit| unless permission.mask & bit != 0 permission.mask = permission.mask | bit end end - permission.save! + if proxy_owner.new_record? + permission.permitted_object = proxy_owner + proxy_owner.permissions.push(permission) + else + permission.save! + end permission + # raise 'set' end def can?(*args) options = args.extract_options! options.assert_valid_keys(:in, :on) @@ -52,12 +59,16 @@ !on(options[:on]).granted(bits_for(options[:on], args)).empty? end def revoke(*args) options = args.extract_options! - if args.length == 1 && args.first == :all - on(options[:on]).destroy_all + if args.first == :all + if options[:on] + on(options[:on]).destroy_all + else + reload.destroy_all + end else bits = bits_for(options[:on], args) on(options[:on]).each do |permission| bits.each do |bit| if permission.mask & bit @@ -69,33 +80,20 @@ end end def bits_for(scope, permissions) on = PermissionDefinition.normalize_scope(proxy_owner.class, scope) - permissions.map do |permission| + permissions.flatten.map do |permission| proxy_owner.class.permissions[on].try(:permissions).try(:[], permission.to_s.underscore.gsub('/', '_').to_sym) || raise(Permissive::InvalidPermissionError.new("#{proxy_owner.class.name} does not have a#{'n' if permission.to_s[0, 1].downcase =~ /[aeiou]/} #{permission} permission#{" on #{on}" if on}")) end end private :bits_for end delegate :can!, :can?, :revoke, :to => :permissions permission_definition = Permissive::PermissionDefinition.define(self, options, &block) - - permission_setter = options[:on].nil? || options[:on] == :global ? 'permissions=' : "#{options[:on].to_s.singularize}_permissions=" - class_eval <<-eoc - def #{permission_setter}(values) - values ||= [] - if values.all? {|value| value.is_a?(String) || value.is_a?(Symbol)} - can!(values, :reset => true, :on => #{options[:on].inspect}) - else - super(values) - end - end - eoc - - + permission_definition.define_methods # Oh that's right, it'll return an object. permission_definition end alias :has_permission :has_permissions end