Sha256: 4ae7326d4cc8bacd1d6cb7d237128822fdf53746c87217c8cfa54c66699db01d

Contents?: true

Size: 1.43 KB

Versions: 7

Compression:

Stored size: 1.43 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, :current_user).new(model, options[:current_user])
        options[:fields].deep_dup.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])
          raw_value = content.respond_to?(:value) ? content.value : content.to_s
          if raw_value
            value = @sanitizer.sanitize(raw_value)
            [field[:name], value]
          else
            nil
          end
        end
        Hash[*values.compact.flatten(1)]
      end

    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
type_station-0.7.0 lib/type_station/blocks/entity.rb
type_station-0.6.0 lib/type_station/blocks/entity.rb
type_station-0.5.4 lib/type_station/blocks/entity.rb
type_station-0.5.3 lib/type_station/blocks/entity.rb
type_station-0.5.2 lib/type_station/blocks/entity.rb
type_station-0.5.1 lib/type_station/blocks/entity.rb
type_station-0.4.7 lib/type_station/blocks/entity.rb