Sha256: af604a988009010c9d5982c04dcbdc8a123ed803377a6ab6055738d52909e46a

Contents?: true

Size: 1.89 KB

Versions: 8

Compression:

Stored size: 1.89 KB

Contents

module CmAdmin
  module Models
    class Column
      attr_accessor :field_name, :field_type, :header, :format, :prefix, :suffix, :exportable, :round,
      :cm_css_class, :link, :url, :custom_method, :helper_method, :managable, :lockable, :drawer_partial

      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)
      end

      #returns a string value as a header (either field_name or value present in header attribute)
      def format_header
        self.header.present? ? self.header.to_s.gsub(/_/, ' ')&.upcase : self.field_name.to_s.gsub(/_/, ' ').upcase
      end

      def set_default_values
        self.exportable = true
        self.managable = true
        self.lockable = false
      end

      #formatting value for different data types
      def self.format_data_type(column, value)
        case column.column_type
        when :string
          if column.format.present?
            column.format = [column.format] if column.format.is_a? String
            column.format.each do |formatter|
              value = value.send(formatter)
            end
          end
        when :datetime
          format_value = column.format.present? ? column.format.to_s : '%d/%m/%Y'
          value = value.strftime(format_value)
        when :enum
          value = value.titleize
        when :decimal
          round_to = column.round.present? ? column.round.to_i : 2
          value = value.round(round_to)
        when :custom

        end
        return value
      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

8 entries across 8 versions & 1 rubygems

Version Path
cm-admin-0.6.3 lib/cm_admin/models/column.rb
cm-admin-0.6.2 lib/cm_admin/models/column.rb
cm-admin-0.6.1 lib/cm_admin/models/column.rb
cm-admin-0.6.0 lib/cm_admin/models/column.rb
cm-admin-0.5.9 lib/cm_admin/models/column.rb
cm-admin-0.5.8 lib/cm_admin/models/column.rb
cm-admin-0.5.7 lib/cm_admin/models/column.rb
cm-admin-0.5.6 lib/cm_admin/models/column.rb