Module: Hippo::Concerns::ExportMethods::ClassMethods
- Defined in:
- lib/hippo/concerns/export_methods.rb
Instance Method Summary collapse
-
#delegate_and_export(*names) ⇒ Object
Convenience method to create a Rails delegation and export the resulting method.
-
#delegate_and_export_field(target, field, optional: true, limit: nil) ⇒ Object
For situations where the delegate_and_export method guesses wrong on the association and field names would generate a method “purchase_order_visible_id“ that would call “purchase_order_visible.id“ This would do the right thing: class PoLine < Hippo::Model belongs_to :purchase_order delegate_and_export_field :purchase_order, :visible_id end.
-
#export_methods(*method_names) ⇒ Object
Called by a model to export methods to the API An exported method will be called and it’s results returned along with the models JSON representation.
-
#exported_method_dependancies(requested_methods) ⇒ Array
Retrieve the list of dependent associations for the given methods Also includes methods that were exported as “optional: false“.
-
#has_exported_method?(name, user) ⇒ Boolean
Check if the method can be called by user.
Instance Method Details
#delegate_and_export(*names) ⇒ Object
Convenience method to create a Rails delegation and export the resulting method
43 44 45 46 47 48 49 |
# File 'lib/hippo/concerns/export_methods.rb', line 43 def delegate_and_export( *names ) = names. names.each do | name | target,field = name.to_s.split(/_(?=[^_]+(?: |$))| /) delegate_and_export_field( target, field, optional: [:optional], limit: [:limit] ) end end |
#delegate_and_export_field(target, field, optional: true, limit: nil) ⇒ Object
For situations where the delegate_and_export method guesses wrong on the association and field names would generate a method “purchase_order_visible_id“ that would call “purchase_order_visible.id“ This would do the right thing:
class PoLine < Hippo::Model
belongs_to :purchase_order
delegate_and_export_field :purchase_order, :visible_id
end
68 69 70 71 72 73 |
# File 'lib/hippo/concerns/export_methods.rb', line 68 def delegate_and_export_field( target, field, optional: true, limit: nil ) delegate field, to: target, prefix: target, allow_nil: true self.export_methods( "#{target}_#{field}", { depends: target.to_sym, optional: optional, limit: limit } ) end |
#export_methods(*method_names) ⇒ Object
Called by a model to export methods to the API An exported method will be called and it’s results returned along with the models JSON representation.
22 23 24 25 26 27 28 29 |
# File 'lib/hippo/concerns/export_methods.rb', line 22 def export_methods( *method_names ) method_names.flatten! = method_names. self.exported_methods ||= {} method_names.map do | name | exported_methods[ name.to_sym] = end end |
#exported_method_dependancies(requested_methods) ⇒ Array
Retrieve the list of dependent associations for the given methods Also includes methods that were exported as “optional: false“
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/hippo/concerns/export_methods.rb', line 88 def exported_method_dependancies( requested_methods ) requested_methods.map!(&:to_sym) return [] if self.exported_methods.blank? depends = self.exported_methods.each_with_object(Array.new) do | kv, result | ( export, ) = kv if [:depends] && ( false == [:optional] || requested_methods.include?(export) ) result << [:depends] end end depends.uniq end |
#has_exported_method?(name, user) ⇒ Boolean
Check if the method can be called by user
76 77 78 79 80 81 82 |
# File 'lib/hippo/concerns/export_methods.rb', line 76 def has_exported_method?( name, user ) if self.exported_methods && ( = self.exported_methods[ name.to_sym ] ) return evaluate_export_limit( user, :method, name, [:limit] ) else return false end end |