Module: ParserUtils

Included in:
Hario::FilterParser, Hario::PluckParser
Defined in:
lib/hario/behaviours/utils.rb

Constant Summary collapse

InvalidAttributeError =
Class.new(RuntimeError)

Instance Method Summary collapse

Instance Method Details

#end_model_from_association_chain(association_chain) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/hario/behaviours/utils.rb', line 8

def end_model_from_association_chain(association_chain)
  head = @klass

  association_chain.each do |a_name|
    head = head.reflect_on_all_associations.find{ |a| a.name.to_s == a_name }.klass
  end
  
  head
end

#raise_if_unlisted_attribute!(type, model_class, attribute) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/hario/behaviours/utils.rb', line 18

def raise_if_unlisted_attribute!(type, model_class, attribute)
  return unless model_class.respond_to?(:hario_attributes_list)
  return unless model_class.hario_attributes_list
  lists = model_class.hario_attributes_list[type]
  return unless lists
  attribute = attribute.to_sym
  if (lists[:except].present? &&  lists[:except].include?(attribute)) ||
     (lists[:only  ].present? && !lists[:only  ].include?(attribute))
    raise InvalidAttributeError, "#{attribute} is forbidden"
  end
end

#table_name_from_association_chain(association_chain) ⇒ Object



4
5
6
# File 'lib/hario/behaviours/utils.rb', line 4

def table_name_from_association_chain(association_chain)
  end_model_from_association_chain(association_chain).table_name
end