Sha256: bfda4c1f0f7bbe54cad16cc4a25bf7ac1a97cb2f56f6aee26541e2fe63d9b368

Contents?: true

Size: 1.78 KB

Versions: 15

Compression:

Stored size: 1.78 KB

Contents

require_relative 'utils/associations'

module CmAdmin
  module Models
    class Column
      include Utils::Associations

      attr_accessor :field_name, :field_type, :header, :format, :prefix, :suffix, :exportable, :round, :height, :width,
      :cm_css_class, :link, :url, :custom_method, :helper_method, :managable, :lockable, :drawer_partial, :tag_class,
      :display_if, :association_name, :association_type

      def initialize(field_name, attributes = {})
        @field_name = field_name
        set_default_values
        attributes.each do |key, value|
          self.send("#{key.to_s}=", value)
        end

        #formatting header (either field_name or value present in header attribute)
        self.send("header=", format_header)
        self.height = 50 if self.field_type == :image && self.height.nil?
        self.width = 50 if self.field_type == :image && self.width.nil?
        self.display_if = lambda { |arg| return true } if self.display_if.nil?

        validation_for_association
      end

      #returns a string value as a header (either field_name or value present in header attribute)
      def format_header
        header_value = if self.header.present?
                         self.header
                       elsif self.field_type.to_s == 'association'
                         self.association_name
                       else
                         self.field_name
                       end
        header_value.to_s.titleize.upcase
      end

      def set_default_values
        self.exportable = true
        self.managable = true
        self.lockable = false
        self.tag_class = {}
      end

      class << self
        def find_by(model, search_hash)
          model.available_fields.find { |i| i.name == search_hash[:name] }
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
cm-admin-1.1.4 lib/cm_admin/models/column.rb
cm-admin-1.1.3 lib/cm_admin/models/column.rb
cm-admin-1.1.2 lib/cm_admin/models/column.rb
cm-admin-1.1.1 lib/cm_admin/models/column.rb
cm-admin-1.1.0 lib/cm_admin/models/column.rb
cm-admin-1.0.9 lib/cm_admin/models/column.rb
cm-admin-1.0.8 lib/cm_admin/models/column.rb
cm-admin-1.0.7 lib/cm_admin/models/column.rb
cm-admin-1.0.6 lib/cm_admin/models/column.rb
cm-admin-1.0.5 lib/cm_admin/models/column.rb
cm-admin-1.0.3 lib/cm_admin/models/column.rb
cm-admin-1.0.2 lib/cm_admin/models/column.rb
cm-admin-1.0.0 lib/cm_admin/models/column.rb
cm-admin-0.9.1 lib/cm_admin/models/column.rb
cm-admin-0.9.0 lib/cm_admin/models/column.rb