Sha256: 9d501fd10f62057042cadefdde892e28f153873405ab37fff890041cd1fe75a1

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

module Granite
  module Form
    module Model
      module Primary
        extend ActiveSupport::Concern
        DEFAULT_PRIMARY_ATTRIBUTE_OPTIONS = lambda do
          {
            type: Granite::Form::UUID,
            default: -> { Granite::Form::UUID.random_create }
          }
        end

        included do
          class_attribute :_primary_name, instance_writer: false
          delegate :has_primary_attribute?, to: 'self.class'

          prepend PrependMethods
          alias_method :eql?, :==
        end

        module ClassMethods
          def primary(*args)
            options = args.extract_options!
            self._primary_name = (args.first.presence || Granite::Form.primary_attribute).to_s
            unless has_attribute?(_primary_name)
              options[:type] = args.second if args.second
              attribute _primary_name, options.presence || DEFAULT_PRIMARY_ATTRIBUTE_OPTIONS.call
            end
            alias_attribute :primary_attribute, _primary_name
          end

          alias primary_attribute primary

          def has_primary_attribute? # rubocop:disable Naming/PredicateName
            has_attribute? _primary_name
          end

          def primary_name
            _primary_name
          end
        end

        module PrependMethods
          def ==(other)
            if other.instance_of?(self.class) && has_primary_attribute?
              if primary_attribute
                primary_attribute == other.primary_attribute
              else
                object_id == other.object_id
              end
            else
              super(other)
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
granite-form-0.6.0 lib/granite/form/model/primary.rb