Module: AttributeExt::SafeAttributes

Defined in:
lib/attribute_ext/safe_attributes.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_roleObject

Returns default role used by SafeAttributes. See SafeAttributes#default_role= for how to specify a default role.



5
6
7
# File 'lib/attribute_ext/safe_attributes.rb', line 5

def SafeAttributes.default_role
  @default_role || :default
end

.default_role=(role) ⇒ Object

Sets SafeAttributes default role that will be used when given role is a nil value or the :default role. The SafeAttributes default role will only affect this extension and will not be given to Rails 3.1 mass assignment authorizer.



13
14
15
# File 'lib/attribute_ext/safe_attributes.rb', line 13

def SafeAttributes.default_role=(role)
  @default_role = role
end

.included(base) ⇒ Object

:nodoc:



42
43
44
45
# File 'lib/attribute_ext/safe_attributes.rb', line 42

def self.included(base)  # :nodoc:
  base.extend(ClassMethods)
  base.alias_method_chain :mass_assignment_authorizer, :safe_attrs
end

.role_mapper(&block) ⇒ Object

Returns current role mapper block or sets role mapper if an block is given. By default no role mapper is active.

AttributeExt::SafeAttributes.role_mapper do |role|
  [:guest, :user, :admin].include?(role) ? role : :guest
end


24
25
26
27
# File 'lib/attribute_ext/safe_attributes.rb', line 24

def SafeAttributes.role_mapper(&block)
  self.role_mapper = block if block
  @role_mapper
end

.role_mapper=(role_mapper) ⇒ Object

Sets current role mapper to given Proc or removes role mapper if a nil value is given. Any other value will do nothing.

AttributeExt::SafeAttributes.role_mapper = Proc.new do |role|
  [:guest, :user, :admin].include?(role) ? role : :guest
end

See SafeAttributes#role_mapper for an short way to set a role mapper.



37
38
39
40
# File 'lib/attribute_ext/safe_attributes.rb', line 37

def SafeAttributes.role_mapper=(role_mapper)
  @role_mapper = role_mapper if role_mapper.is_a?(Proc)
  @role_mapper = nil if role_mapper.nil?
end

Instance Method Details

#mass_assignment_authorizer_with_safe_attrs(role = nil) ⇒ Object

:nodoc:



99
100
101
# File 'lib/attribute_ext/safe_attributes.rb', line 99

def mass_assignment_authorizer_with_safe_attrs(role = nil) # :nodoc:
  safe_attributes_authorizer role
end

#safe_attribute_names(role = nil) ⇒ Object

Returns an array with attributes allowed to be mass assigned by given role. Role will be mapped before given to rules. This method should only be used to test own rules without need to create lots of records to test different situations. See AttributeExt specs for details.



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/attribute_ext/safe_attributes.rb', line 134

def safe_attribute_names(role = nil)
  role = safe_attributes_role(role)
  
  names = []
  self.class.safe_attributes.collect do |attrs, options|
    next unless options[:as].empty? or options[:as].include?(role)
    next unless options[:if].nil? or safe_attrs_call(options[:if], role)
    next unless options[:unless].nil? or !safe_attrs_call(options[:unless], role)

    names += attrs.collect(&:to_s)
  end
  names.uniq
end

#safe_attributes_authorizer(role = nil) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/attribute_ext/safe_attributes.rb', line 103

def safe_attributes_authorizer(role = nil)
  if AttributeExt.activemodel_3_0?
    attrs      = safe_attribute_names
    authorizer = mass_assignment_authorizer_without_safe_attrs
  else
    attrs      = safe_attribute_names(role)
    authorizer = mass_assignment_authorizer_without_safe_attrs(role)
  end

  if authorizer.kind_of?(::ActiveModel::MassAssignmentSecurity::WhiteList)
    return authorizer + attrs
  else
    return ::ActiveModel::MassAssignmentSecurity::WhiteList.new attrs
  end
end

#safe_attributes_role(role = nil) ⇒ Object

Returns new mapped role for given role used by SafeAttributes. This method should only be used to test own role mapper implementations without need for a full application. See AttributeExt specs for details.

See role_mapper method in SafeAttributes module for how to set a role mapper.



124
125
126
127
128
# File 'lib/attribute_ext/safe_attributes.rb', line 124

def safe_attributes_role(role = nil)
  return AttributeExt::SafeAttributes.role_mapper.call(role) unless AttributeExt::SafeAttributes.role_mapper.nil?
  return AttributeExt::SafeAttributes.default_role if role.nil? or role == :default
  role
end