Sha256: ee0870a01b3f0842e80878d475a0adbf1ece29b6d9173d8040f9c894634cf862

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true

class <%= @index_name %> < <%= @base_class %>
  # plugin :active_record
  # plugin :sequel

  <%- @types.each do |type| -%>
  define_type :<%= type %> do
    # Collection
    # ==========
    #
    # Collection wraps the data into an array of items that should be serialized. The first argument that is
    # yielded must extends Enumerable.
    # Useful for eager loading data from database or any other repository. Below is an example of a rails like
    # application could load using activerecord.
    #
    #   collection do |conditions, &block|
    #     context = {}
    #     <%= type.camelize %>.where(conditions).find_in_batches(batch_size: 5000) do |batch|
    #       block.call batch, context, ...
    #     end
    #   end
    #
    #
    # Serializer
    # ==========
    #
    # The serializer can be any class that respond with the `as_json` class method.
    # And the result of its as_json is a Hash.
    #
    # Here is an example of a simple serializer:
    # app/serializers/<%= type %>_serializer.rb
    #   class <%= type.camelize %>Serializer
    #     def initialize(<%= type %>, _context)
    #       @<%= type %> = <%= type %>
    #     end
    #
    #     def as_json
    #       { '_id' => @<%= type %>.id, 'name' => @<%= type %>.name }
    #     end
    #   end
    #
    # And here you specify your serializer classe.
    #    serializer Serializers::<%= type.camelize %>Serializer
    #
    # You can also serialize the collection entry using a block:
    #
    #   serializer do |model, context = {}|
    #     hash = {
    #       name: <%= type %>.name,
    #     }
    #     # Context is just an example here. But it's useful for eager loading data.
    #     # I'll think a better example when implement this idea.
    #     hash[:some_attribute] = <%= type %>.some_attribute if context[:include_some_attribute]
    #     hash
    #   end
  end
  <%- end -%>
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
esse-0.0.5 lib/esse/cli/templates/index.rb.erb