Sha256: f1b1968fd62c82f5761efcbaa3aa084dafa085b5a26d49ed5dace2fc9e6520c8
Contents?: true
Size: 1.92 KB
Versions: 1
Compression:
Stored size: 1.92 KB
Contents
# frozen_string_literal: true require 'active_support/concern' module HasCalculatedFields module HasCalculatedFields extend ActiveSupport::Concern module ClassMethods def has_calculated_fields(args = {}) define_method :has_calculated_fields_options do self.class.instance_variable_get("@has_calculated_fields_options") end class_eval do @has_calculated_fields_options = { }.merge(args) def self.has_calculated_fields? true end end end end included do before_save :_has_calculated_fields__on_before_save end def _has_calculated_fields__on_before_save return unless respond_to?(:has_calculated_fields_options) return unless has_calculated_fields_options.has_key?(:on_before_save) has_calculated_fields_options[:on_before_save].each do |data| attr_equal = "#{data[:calculated_field]}=" value = case data[:type] when :date if data[:format] I18n.l(_has_calculated_fields__field_value(data), format: data[:format]) else I18n.l(_has_calculated_fields__field_value(data)) end when :method data[:method].call(self) end send(attr_equal, value) end end def _has_calculated_fields__field_value(data) field = data.fetch(:field, nil) if field.present? send(field) || _has_calculated_fields__default_field_value(data) else _has_calculated_fields__default_field_value(data) end end def _has_calculated_fields__default_field_value(data) value = data.fetch(:default, nil) case value when :now Time.current when :nil nil else value end end end end ActiveRecord::Base.send(:include, HasCalculatedFields::HasCalculatedFields)
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
has_calculated_fields-1.0.1 | lib/has_calculated_fields/has_calculated_fields.rb |