Sha256: f2136a9d1b886853996e35ba175f0053f491917966cb45d05f85909416374efe

Contents?: true

Size: 1.94 KB

Versions: 28

Compression:

Stored size: 1.94 KB

Contents

module Manage
  module Fields
    class Reader
      class << self
        def field_value(scope, field_data)
          current_field, rest_field_parts, custom_query, custom_format = _parse_field_data(field_data)
          value = scope.public_send(current_field)

          if _is_field_relation?(value)
            value = custom_query.present? ? custom_query.call(value) : value

            value.map do |entity|
              ("<a href=\"#{current_field}/#{entity.id}\">#{custom_format.present? ? custom_format.call(entity) : entity.id }</a>")
            end.join(', ').html_safe
          else
            rest_field_parts.empty? ? value : field_value(value, rest_field_parts.join('.'))
          end
        end

        def field_title(resource_class, field_data, prefix='')
          current_field, rest_field_parts = _parse_field_data(field_data)
          title = resource_class.human_attribute_name(current_field)

          rest_field_parts.empty? ? prefix + title : field_title(resource_class, rest_field_parts.join('.'), prefix + title + ' ')
        end

        private
        def _is_field_relation?(field_value)
          field_value.is_a?(ActiveRecord::Associations::CollectionProxy)
        end

        def _parse_field_data(field_data)
          custom_query = nil
          custom_format = nil

          if field_data.is_a?(Hash)
            current_field = field_data.keys.first
            rest_field_parts = field_data[current_field]

            if rest_field_parts.is_a?(String)
              rest_field_parts = Array(rest_field_parts)
            else
              custom_query = field_data[current_field][:query]
              custom_format = field_data[current_field][:format]
              rest_field_parts = []
            end
          else
            current_field, *rest_field_parts = field_data.to_s.split('.')
          end

          return current_field, rest_field_parts, custom_query, custom_format
        end

      end
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
manage-1.3.31 lib/manage/fields/reader.rb
manage-1.3.29 lib/manage/fields/reader.rb
manage-1.3.28 lib/manage/fields/reader.rb
manage-1.3.27 lib/manage/fields/reader.rb
manage-1.3.26 lib/manage/fields/reader.rb
manage-1.3.25 lib/manage/fields/reader.rb
manage-1.3.24 lib/manage/fields/reader.rb
manage-1.3.20 lib/manage/fields/reader.rb
manage-1.3.19 lib/manage/fields/reader.rb
manage-1.3.18 lib/manage/fields/reader.rb
manage-1.3.17 lib/manage/fields/reader.rb
manage-1.3.16 lib/manage/fields/reader.rb
manage-1.3.15 lib/manage/fields/reader.rb
manage-1.3.14 lib/manage/fields/reader.rb
manage-1.3.13 lib/manage/fields/reader.rb
manage-1.3.12 lib/manage/fields/reader.rb
manage-1.3.11 lib/manage/fields/reader.rb
manage-1.3.10 lib/manage/fields/reader.rb
manage-1.3.9 lib/manage/fields/reader.rb
manage-1.3.8 lib/manage/fields/reader.rb