Sha256: 5c6e0bac72e70b2c787899552f20be5a52741cfe31ff06b3604864cb01aacd68

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

require 'type_station/blocks/base'

module TypeStation
  module Blocks
    class Entity < Base

      def data
        {
          action: options[:action],
          id: options[:model_id] || model.to_param,
          type: (options[:type] || model._type).to_s.classify,
          parent_id: options[:model_parent_id] || model.parent_id.to_s,
          fields: model_fields,
          values: model_values,
          create_url: options[:create_url],
          position: model.position,
          options: options[:options] || data_options
        }
      end

      private

      def data_options
        nil
      end

      def render_default(content)
        nil
      end

      def model_fields
        lambda_binging = Struct.new(:model).new(model)
        options[:fields].map do |field|
          if field[:options] && field[:options].is_a?(Proc)
            field[:options] = lambda_binging.instance_exec(&field[:options])
          end
          field
        end
      end

      def model_values
        values = options[:fields].map do |field|
          content = model.get(field[:name])
          value = content.respond_to?(:value) ? content.value : content.to_s
          if value
            [field[:name], value]
          else
            nil
          end
        end
        Hash[*values.compact.flatten(1)]
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
type_station-0.2.3 lib/type_station/blocks/entity.rb
type_station-0.2.2 lib/type_station/blocks/entity.rb