Class: CanCan::EditRule
- Inherits:
-
Object
- Object
- CanCan::EditRule
- Defined in:
- lib/cancan_edit/edit_rule.rb
Instance Attribute Summary collapse
-
#base_behavior ⇒ Object
readonly
Returns the value of attribute base_behavior.
-
#conditions ⇒ Object
readonly
Returns the value of attribute conditions.
-
#expanded_fields ⇒ Object
writeonly
Sets the attribute expanded_fields.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#subjects ⇒ Object
readonly
Returns the value of attribute subjects.
Instance Method Summary collapse
- #conditions_empty? ⇒ Boolean
-
#initialize(base_behavior, field, subject, conditions, block) ⇒ EditRule
constructor
A new instance of EditRule.
- #matches_conditions?(field, subject, extra_args) ⇒ Boolean
- #only_block? ⇒ Boolean
- #only_raw_sql? ⇒ Boolean
- #relevant?(field, subject) ⇒ Boolean
Constructor Details
#initialize(base_behavior, field, subject, conditions, block) ⇒ EditRule
Returns a new instance of EditRule.
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_behavior ⇒ Object (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 |
#conditions ⇒ Object (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
4 5 6 |
# File 'lib/cancan_edit/edit_rule.rb', line 4 def (value) @expanded_fields = value end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
3 4 5 |
# File 'lib/cancan_edit/edit_rule.rb', line 3 def fields @fields end |
#subjects ⇒ Object (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
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
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
35 36 37 |
# File 'lib/cancan_edit/edit_rule.rb', line 35 def only_block? conditions_empty? && !@block.nil? end |
#only_raw_sql? ⇒ 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
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 |