module AdminIt::MongoidData::Field

Attributes

assoc[RW]

Public Class Methods

included(base) click to toggle source
# File lib/admin_it/data/mongoid/field.rb, line 5
def self.included(base)
  base.class_eval do
    class << self
      attr_accessor :assoc
    end
    class_attr_reader :assoc
  end
end

Protected Instance Methods

read_value(entity) click to toggle source
# File lib/admin_it/data/mongoid/field.rb, line 16
def read_value(entity)
  value = entity.send(name)
  if type == :relation
    if assoc.collection?
      value.nil? || value.empty? ? [] : value.map(&:id).to_json
    else
      value.nil? ? nil : value.id
    end
  else
    value
  end
end
show_value(entity) click to toggle source
# File lib/admin_it/data/mongoid/field.rb, line 29
def show_value(entity)
  value = entity.send(name)
  if type == :relation
    resource = AdminIt.resources.values.find do |r|
      r.entity_class == assoc.klass
    end
    return I18n.t('admin_it.relation.no_resource') if resource.nil?
    context = resource.contexts.find { |c| c <= ShowContext }
    return I18n.t('admin_it.relation.no_context') if context.nil?
    if assoc.collection?
      if value.count == 0
        I18n.t('admin_it.collection.no_data')
      else
        v = context.read(value.first)
        v.nil? ? '' : context.read(value.first) + ' ...'
      end
    else
      context.read(value)
    end
  else
    value
  end
end
write_value(entity, value) click to toggle source
# File lib/admin_it/data/mongoid/field.rb, line 53
def write_value(entity, value)
  entity.send("#{name}=", value)
end