module ForestLiana::Collection::ClassMethods

Attributes

active_record_class[RW]
collection_name[RW]
is_read_only[RW]
is_searchable[RW]

Public Instance Methods

action(name, opts = {}) click to toggle source
# File lib/forest_liana/collection.rb, line 26
def action(name, opts = {})
  opts[:name] = name
  model.actions << ForestLiana::Model::Action.new(opts)
end
belongs_to(name, opts, &block) click to toggle source
# File lib/forest_liana/collection.rb, line 128
def belongs_to(name, opts, &block)
  model.fields << opts.merge({
    field: name,
    is_virtual: true,
    is_searchable: false,
    type: 'String'
  })

  define_method(name) { self.data[name] } if smart_collection?

  if serializer_name && ForestLiana::UserSpace.const_defined?(
      serializer_name)
    ForestLiana::UserSpace.const_get(serializer_name).class_eval do
      has_one(name, name: name, include_data: true, &block)
    end
  end
end
collection(collection_name, opts = {}) click to toggle source
# File lib/forest_liana/collection.rb, line 10
def collection(collection_name, opts = {})
  self.collection_name = find_name(collection_name).to_s
  self.is_read_only = opts[:read_only] || false
  self.is_searchable = opts[:is_searchable] || false

  # NOTICE: Creates dynamically the serializer if it's a Smart Collection.
  if smart_collection? &&
      !ForestLiana::UserSpace.const_defined?(serializer_name)

    ForestLiana.names_overriden[self] = self.collection_name.to_s

    ForestLiana::SerializerFactory.new(is_smart_collection: true)
      .serializer_for(self)
  end
end
field(name, opts, &block) click to toggle source
# File lib/forest_liana/collection.rb, line 40
def field(name, opts, &block)
  # TODO: Handle empty name

  if opts.key?(:isRequired)
    FOREST_LOGGER.warn "DEPRECATION WARNING: isRequired on field \"#{name}\" is deprecated. Please use is_required."
    opts[:is_required] = !!opts[:isRequired]
    opts.delete(:isRequired)
  end
  if opts.key?(:isReadOnly)
    FOREST_LOGGER.warn "DEPRECATION WARNING: isReadOnly on field \"#{name}\" is deprecated. Please use is_read_only."
    opts[:is_read_only] = !!opts[:isReadOnly]
    opts.delete(:isReadOnly)
  end
  if opts.key?(:isFilterable)
    FOREST_LOGGER.warn "DEPRECATION WARNING: isFilterable on field \"#{name}\" is deprecated. Please use is_filterable."
    opts[:is_filterable] = !!opts[:isFilterable]
    opts.delete(:isFilterable)
  end
  if opts.key?(:isSortable)
    FOREST_LOGGER.warn "DEPRECATION WARNING: isSortable on field \"#{name}\" is deprecated. Please use is_sortable."
    opts[:is_sortable] = !!opts[:isSortable]
    opts.delete(:isSortable)
  end

  opts[:is_read_only] = true unless opts.has_key?(:is_read_only)
  opts[:is_read_only] = false if opts.has_key?(:set)
  opts[:is_required] = false unless opts.has_key?(:is_required)
  opts[:type] = "String" unless opts.has_key?(:type)
  opts[:default_value] = nil unless opts.has_key?(:default_value)
  opts[:integration] = nil unless opts.has_key?(:integration)
  opts[:reference] = nil unless opts.has_key?(:reference)
  opts[:inverse_of] = nil unless opts.has_key?(:inverse_of)
  opts[:relationships] = nil unless opts.has_key?(:relationships)
  opts[:widget] = nil unless opts.has_key?(:widget)
  opts[:validations] = [] unless opts.has_key?(:validations)
  opts[:is_virtual] = true unless opts.has_key?(:polymorphic_key) && opts[:polymorphic_key]

  model.fields << opts.merge({
    field: name,
    is_filterable: !!opts[:is_filterable],
    is_sortable: !!opts[:is_sortable],
  })

  define_method(name) { self.data[name] } if smart_collection?

  if serializer_name && ForestLiana::UserSpace.const_defined?(
      serializer_name)
    ForestLiana::UserSpace.const_get(serializer_name).class_eval do
      if block
        # NOTICE: Smart Field case.
        compute_value = lambda do |object|
          begin
            object.instance_eval(&block)
          rescue => exception
            FOREST_REPORTER.report exception
            FOREST_LOGGER.error "Cannot retrieve the " + name.to_s + " value because of an " \
              "internal error in the getter implementation: " + exception.message
            nil
          end
        end

        attribute(name, &compute_value)
      else
        # NOTICE: Smart Collection field case.
        attribute(name)
      end
    end
  end
end
has_many(name, opts, &block) click to toggle source
# File lib/forest_liana/collection.rb, line 110
def has_many(name, opts, &block)
  model.fields << opts.merge({
    field: name,
    is_virtual: true,
    is_searchable: false,
    type: ['String']
  })

  define_method(name) { self.data[name] } if smart_collection?

  if serializer_name && ForestLiana::UserSpace.const_defined?(
      serializer_name)
    ForestLiana::UserSpace.const_get(serializer_name).class_eval do
      has_many(name, name: name)
    end
  end
end
search_fields(fields) click to toggle source
# File lib/forest_liana/collection.rb, line 36
def search_fields(fields)
  model.search_fields = fields
end
segment(name, opts = {}, &block) click to toggle source
# File lib/forest_liana/collection.rb, line 31
def segment(name, opts = {}, &block)
  opts[:name] = name
  model.segments << ForestLiana::Model::Segment.new(opts, &block)
end

Private Instance Methods

find_name(collection_name) click to toggle source
# File lib/forest_liana/collection.rb, line 148
def find_name(collection_name)
  # TODO: Remove once lianas prior to 2.0.0 are not supported anymore.
  model = ForestLiana.models.find { |collection| collection.try(:table_name) == collection_name.to_s }
  if model
    collection_name_new = ForestLiana.name_for(model)
    FOREST_LOGGER.warn "DEPRECATION WARNING: Collection names are now based on the models " \
      "names. Please rename the collection '#{collection_name.to_s}' of your Forest " \
      "customisation in '#{collection_name_new}'."
    return collection_name_new
  end

  # TODO: Remove once lianas prior to 2.0.0 are not supported anymore.
  model = ForestLiana.names_old_overriden.invert[collection_name.to_s]
  if model
    collection_name_new = ForestLiana.name_for(model)
    FOREST_LOGGER.warn "DEPRECATION WARNING: Collection names are now based on the models " \
      "names. Please rename the collection '#{collection_name.to_s}' of your Forest " \
      "customisation in '#{collection_name_new}'."
    return collection_name_new
  end

  collection_name.to_s
end
model() click to toggle source
# File lib/forest_liana/collection.rb, line 172
def model
  collection = ForestLiana.apimap.find do |object|
    object.name.to_s == self.collection_name.try(:to_s)
  end

  if collection.blank?
    collection = ForestLiana::Model::Collection.new({
      name: self.collection_name.to_s,
      is_read_only: self.is_read_only,
      is_searchable: self.is_searchable,
      is_virtual: true,
      fields: []
    })

    ForestLiana.apimap << collection
  end

  collection
end
serializer_name() click to toggle source
# File lib/forest_liana/collection.rb, line 197
def serializer_name
  if smart_collection?
    "#{collection_name.to_s.classify}Serializer"
  else
    component_prefix = ForestLiana.component_prefix(active_record_class)
    serializer_name = "#{component_prefix}Serializer"

    "ForestLiana::UserSpace::#{serializer_name}"
  end
end
serializer_name_for_reference(reference) click to toggle source
# File lib/forest_liana/collection.rb, line 208
def serializer_name_for_reference(reference)
  association = opts[:reference].split('.').first
  component_prefix = association.classify
  serializer_name = "#{component_prefix}Serializer"

  "ForestLiana::UserSpace::#{serializer_name}"
end
smart_collection?() click to toggle source
# File lib/forest_liana/collection.rb, line 216
def smart_collection?
  !active_record_class
end