Method: ActiveJsonModel::Array::ClassMethods#json_polymorphic_via

Defined in:
lib/active_json_model/array.rb

#json_polymorphic_via(&block) ⇒ Object

Define a polymorphic factory to choose the concrete class for the list model. Note that because the array_data passed to the block is an array of models, you must account for what the behavior is if there are no elements.

Example:

class BaseWorkflowArray
  include ::ActiveJsonModel::List

  json_polymorphic_via do |array_data|
    if array_data[0]
      if array_data[0][:type] == 'email'
        EmailWorkflow
      else
        WebhookWorkflow
      end
    else
      BaseWorkflowArray
    end
  end
end

class EmailWorkflow < BaseWorkflow
  def home_emails
    filter{|e| e.label == 'home'}
  end
end

class WebhookWorkflow < BaseWorkflow
  def secure_webhooks
    filter{|wh| wh.secure }
  end
end


640
641
642
# File 'lib/active_json_model/array.rb', line 640

def json_polymorphic_via(&block)
  @__active_json_model_polymorphic_factory = block
end