Module: BarkestCore::NamedModel

Included in:
AccessGroup, User
Defined in:
lib/barkest_core/concerns/named_model.rb

Overview

Defines a few extensions to models that have unique names.

If the unique field is named :name then you don’t have to do anything more than include this file. For other unique fields simply define UNIQUE_STRING_FIELD to the name of the unique field as either a symbol or a string.

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/barkest_core/concerns/named_model.rb', line 13

def self.included(base)

  base.class_eval do

    private

    def self.unique_string_field
      @unique_string_field ||= const_defined?(:UNIQUE_STRING_FIELD) ? const_get(:UNIQUE_STRING_FIELD).to_sym : :name
    end

    public

    ##
    # Locates the model from ID or name.
    def self.get(value)
      if value.is_a?(Numeric)
        self.find_by(id: value)
      elsif value.is_a?(String)
        self.where("LOWER(\"#{table_name}\".\"#{unique_string_field}\")=?", value.downcase).first
      elsif value.is_a?(Symbol)
        self.where("LOWER(\"#{table_name}\".\"#{unique_string_field}\")=?", value.to_s.downcase).first ||
            self.where("LOWER(\"#{table_name}\".\"#{unique_string_field}\")=?", value.to_s.humanize.downcase).first
      elsif value.class == self
        value
      else
        nil
      end
    end

    ##
    # Gets the name of this model.
    def to_s
      send( self.class.unique_string_field )
    end

    ##
    # Tests for equality on ID or name.
    def ==(other)
      if other.is_a?(Numeric)
        id == other
      elsif other.is_a?(String)
        send( self.class.unique_string_field ).to_s.downcase == other.downcase
      elsif other.is_a?(Symbol)
        [
            other.to_s.downcase,
            other.to_s.humanize.downcase
        ].include?(send( self.class.unique_string_field ).to_s.downcase)
      elsif other.is_a?(self.class)
        id == other.id
      else
        other = self.class.get(other)
        other && id == other.id
      end
    end

  end

end