Sha256: 8049573adfd91cb396f7511b58913a8a4922b1cfa9ba33533c0af89dbae805f3

Contents?: true

Size: 1.9 KB

Versions: 3

Compression:

Stored size: 1.9 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|
    #     context = {}
    #     <%= type.camelize %>.where(conditions).find_in_batches(batch_size: 5000) do |batch|
    #       yield 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 <%= @index_name %>::Serializers::<%= type.camelize %>Serializer
    #
    # You can also serialize the collection entry using a block:
    #
    #   serializer(<%= type %>, context = {}) do
    #     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

3 entries across 3 versions & 1 rubygems

Version Path
esse-0.0.4 lib/esse/cli/templates/index.rb.erb
esse-0.0.3 lib/esse/cli/templates/index.rb.erb
esse-0.0.2 lib/esse/cli/templates/index.rb.erb