Module: Hippo::Concerns::ExportMethods::ClassMethods

Defined in:
lib/hippo/concerns/export_methods.rb

Instance Method Summary collapse

Instance Method Details

#delegate_and_export(*names) ⇒ Object

Convenience method to create a Rails delegation and export the resulting method

Examples:

class Foo < Hippo::Model
      belongs_to :bar
      delegate_and_export :bar_name, :optional=>false
end

foo = Foo.new
foo.bar_name #=> calls foo.bar.name
Foo.has_exported_method?( :bar_name )  #=> true
hippo_to_json( foo ) #=> will first load the bar association, then
                       call foo.bar_name and include it's result in the JSON

43
44
45
46
47
48
49
# File 'lib/hippo/concerns/export_methods.rb', line 43

def delegate_and_export( *names )
    options = names.extract_options!
    names.each do | name |
        target,field = name.to_s.split(/_(?=[^_]+(?: |$))| /)
        delegate_and_export_field( target, field, optional: options[:optional], limit: options[: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

Examples:

class PoLine < Hippo::Model
      belongs_to :purchase_order
      delegate_and_export :purchase_order_visible_id
end

Parameters:

  • target (Association)

    association to delegate to

  • field (Symbol)

    method on Association to call

  • optional (Boolean) (defaults to: true)

    should the method be called and results included all the time, or only if specifically requested

  • limit (Symbol referring to a method, lambda) (defaults to: nil)

    restrict to Users for whom true is returned


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.

Parameters:

  • options (Hash)

    a customizable set of options


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!
    options = method_names.extract_options!
    self.exported_methods ||= {}
    method_names.map do | name |
        exported_methods[ name.to_sym] = options
    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“

Parameters:

  • requested_methods (Array)

    methods that the user has requested be executed and added to the JSON payload

Returns:

  • (Array)

    list of associations that should be included in query


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, options ) = kv
        if options[:depends] && ( false == options[:optional] || requested_methods.include?(export) )
            result << options[:depends]
        end
    end
    depends.uniq
end

#has_exported_method?(name, user) ⇒ Boolean

Check if the method can be called by user

Returns:

  • (Boolean)

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 && ( method_options = self.exported_methods[ name.to_sym ] )
        return evaluate_export_limit( user, :method, name, method_options[:limit] )
    else
        return false
    end
end