Sha256: a213e3e249f99299c0eea38af2d000b1e448368779986ab4750909f7049bbdc7

Contents?: true

Size: 1.88 KB

Versions: 10

Compression:

Stored size: 1.88 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

      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

10 entries across 10 versions & 1 rubygems

Version Path
cm-admin-0.5.5 lib/cm_admin/models/column.rb
cm-admin-0.5.4 lib/cm_admin/models/column.rb
cm-admin-0.5.3 lib/cm_admin/models/column.rb
cm-admin-0.5.2 lib/cm_admin/models/column.rb
cm-admin-0.5.0 lib/cm_admin/models/column.rb
cm-admin-0.4.4 lib/cm_admin/models/column.rb
cm-admin-0.4.3 lib/cm_admin/models/column.rb
cm-admin-0.4.2 lib/cm_admin/models/column.rb
cm-admin-0.4.1 lib/cm_admin/models/column.rb
cm-admin-0.4.0 lib/cm_admin/models/column.rb