Class: ParamAccessible::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/param_accessible/rule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Rule

Returns a new instance of Rule.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/param_accessible/rule.rb', line 9

def initialize *args
  if args.length > 1 && args.last.is_a?(Hash)
    options = args.last
    attributes = args[0..-2]
    
    options.assert_valid_keys :if, :unless, :only, :except
    # options = normalize_options options
  else
    options = {}
    attributes = args
  end
  
  @if_option = options[:if]
  @unless_option = options[:unless]
  
  @only_options = clean_action_option options[:only]
  @except_options = clean_action_option options[:except]
  
  @attributes = normalize_params attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



5
6
7
# File 'lib/param_accessible/rule.rb', line 5

def attributes
  @attributes
end

#except_optionsObject (readonly)

Returns the value of attribute except_options.



7
8
9
# File 'lib/param_accessible/rule.rb', line 7

def except_options
  @except_options
end

#if_optionObject (readonly)

Returns the value of attribute if_option.



6
7
8
# File 'lib/param_accessible/rule.rb', line 6

def if_option
  @if_option
end

#only_optionsObject (readonly)

Returns the value of attribute only_options.



7
8
9
# File 'lib/param_accessible/rule.rb', line 7

def only_options
  @only_options
end

#unless_optionObject (readonly)

Returns the value of attribute unless_option.



6
7
8
# File 'lib/param_accessible/rule.rb', line 6

def unless_option
  @unless_option
end

Instance Method Details

#accessible_params_for(controller, dest) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/param_accessible/rule.rb', line 36

def accessible_params_for controller, dest
  return if @if_option != nil && !controller.send(@if_option)
  return if @unless_option != nil && controller.send(@unless_option)
  
  return if @only_options != nil && !@only_options.include?(controller.action_name)
  return if @except_options != nil && @except_options.include?(controller.action_name)
  
  accessible_hash_for controller.params, @attributes, dest
end

#clean_action_option(value) ⇒ Object



30
31
32
33
34
# File 'lib/param_accessible/rule.rb', line 30

def clean_action_option value
  return if value == nil
  value = [value] unless value.is_a?(Array)
  value.collect {|v| v.to_s }
end