Class: CanCan::EditRule

Inherits:
Object
  • Object
show all
Defined in:
lib/cancan_edit/edit_rule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_behavior, field, subject, conditions, block) ⇒ EditRule

Returns a new instance of EditRule.

Raises:

  • (Error)


6
7
8
9
10
11
12
13
14
# File 'lib/cancan_edit/edit_rule.rb', line 6

def initialize(base_behavior, field, subject, conditions, block)
  raise Error, "You are not able to supply a block with a hash of conditions in #{field} #{subject} ability. Use either one." if conditions.kind_of?(Hash) && !block.nil?
  @match_all = field.nil? && subject.nil?
  @base_behavior = base_behavior
  @fields = [field].flatten
  @subjects = [subject].flatten
  @conditions = conditions || {}
  @block = block
end

Instance Attribute Details

#base_behaviorObject (readonly)

Returns the value of attribute base_behavior.



3
4
5
# File 'lib/cancan_edit/edit_rule.rb', line 3

def base_behavior
  @base_behavior
end

#conditionsObject (readonly)

Returns the value of attribute conditions.



3
4
5
# File 'lib/cancan_edit/edit_rule.rb', line 3

def conditions
  @conditions
end

#expanded_fields=(value) ⇒ Object (writeonly)

Sets the attribute expanded_fields

Parameters:

  • value

    the value to set the attribute expanded_fields to.



4
5
6
# File 'lib/cancan_edit/edit_rule.rb', line 4

def expanded_fields=(value)
  @expanded_fields = value
end

#fieldsObject (readonly)

Returns the value of attribute fields.



3
4
5
# File 'lib/cancan_edit/edit_rule.rb', line 3

def fields
  @fields
end

#subjectsObject (readonly)

Returns the value of attribute subjects.



3
4
5
# File 'lib/cancan_edit/edit_rule.rb', line 3

def subjects
  @subjects
end

Instance Method Details

#conditions_empty?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/cancan_edit/edit_rule.rb', line 43

def conditions_empty?
  @conditions == {} || @conditions.nil?
end

#matches_conditions?(field, subject, extra_args) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cancan_edit/edit_rule.rb', line 21

def matches_conditions?(field, subject, extra_args)
  if @match_all
    call_block_with_all(field, subject, extra_args)
  elsif @block && !subject_class?(subject)
    @block.call(subject, *extra_args)
  elsif @conditions.kind_of?(Hash) && subject.class == Hash
    nested_subject_matches_conditions?(subject)
  elsif @conditions.kind_of?(Hash) && !subject_class?(subject)
    matches_conditions_hash?(subject)
  else
    @conditions.empty? ? true : @base_behavior
  end
end

#only_block?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/cancan_edit/edit_rule.rb', line 35

def only_block?
  conditions_empty? && !@block.nil?
end

#only_raw_sql?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/cancan_edit/edit_rule.rb', line 39

def only_raw_sql?
  @block.nil? && !conditions_empty? && !@conditions.kind_of?(Hash)
end

#relevant?(field, subject) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
# File 'lib/cancan_edit/edit_rule.rb', line 16

def relevant?(field, subject)
  subject = subject.values.first if subject.class == Hash
  @match_all || (matches_field?(field) && matches_subject?(subject))
end