Class: ActiveRecord::Base

Inherits:
Object show all
Defined in:
lib/brick/extensions.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._assoc_namesObject



62
63
64
# File 'lib/brick/extensions.rb', line 62

def self._assoc_names
  @_assoc_names ||= {}
end

.brick_descrip(obj, data = nil, pk_alias = nil) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/brick/extensions.rb', line 127

def self.brick_descrip(obj, data = nil, pk_alias = nil)
  if (dsl = ::Brick.config.model_descrips[(klass = self).name] || klass.brick_get_dsl)
    idx = -1
    caches = {}
    output = +''
    is_brackets_have_content = false
    bracket_name = nil
    dsl.each_char do |ch|
      if bracket_name
        if ch == ']' # Time to process a bracketed thing?
          datum = if data
                    data[idx += 1].to_s
                  else
                    obj_name = +''
                    this_obj = obj
                    bracket_name.split('.').each do |part|
                      obj_name += ".#{part}"
                      this_obj = if caches.key?(obj_name)
                                   caches[obj_name]
                                 else
                                   (caches[obj_name] = this_obj&.send(part.to_sym))
                                 end
                    end
                    this_obj&.to_s || ''
                  end
          is_brackets_have_content = true unless (datum).blank?
          output << (datum || '')
          bracket_name = nil
        else
          bracket_name << ch
        end
      elsif ch == '['
        bracket_name = +''
      else
        output << ch
      end
    end
    output += bracket_name if bracket_name
  end
  if is_brackets_have_content
    output
  elsif pk_alias
    if (id = obj.send(pk_alias))
      "#{klass.name} ##{id}"
    end
  # elsif klass.primary_key
  #   "#{klass.name} ##{obj.send(klass.primary_key)}"
  else
    obj.to_s
  end
end

.brick_get_dslObject

Used to show a little prettier name for an object



71
72
73
74
75
76
77
78
79
80
# File 'lib/brick/extensions.rb', line 71

def self.brick_get_dsl
  # If there's no DSL yet specified, just try to find the first usable column on this model
  unless (dsl = ::Brick.config.model_descrips[name])
    descrip_col = (columns.map(&:name) - _brick_get_fks -
                  (::Brick.config. || []) -
                  [primary_key]).first
    dsl = ::Brick.config.model_descrips[name] = "[#{descrip_col}]" if descrip_col
  end
  dsl
end

.brick_parse_dsl(build_array = nil, prefix = [], translations = {}) ⇒ Object

Pass in true or a JoinArray



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/brick/extensions.rb', line 83

def self.brick_parse_dsl(build_array = nil, prefix = [], translations = {})
  build_array = ::Brick::JoinArray.new.tap { |ary| ary.replace([build_array]) } if build_array.is_a?(::Brick::JoinHash)
  build_array = ::Brick::JoinArray.new unless build_array.nil? || build_array.is_a?(Array)
  members = []
  bracket_name = nil
  prefix = [prefix] unless prefix.is_a?(Array)
  if (dsl = ::Brick.config.model_descrips[name] || brick_get_dsl)
    klass = nil
    dsl.each_char do |ch|
      if bracket_name
        if ch == ']' # Time to process a bracketed thing?
          parts = bracket_name.split('.')
          first_parts = parts[0..-2].map { |part| klass = klass.reflect_on_association(part_sym = part.to_sym).klass; part_sym }
          parts = prefix + first_parts + [parts[-1]]
          if parts.length > 1
            s = build_array
            parts[0..-3].each { |v| s = s[v.to_sym] }
            s[parts[-2]] = nil # unless parts[-2].empty? # Using []= will "hydrate" any missing part(s) in our whole series
            translations[parts[0..-2].join('.')] = klass
          end
          members << parts
          bracket_name = nil
        else
          bracket_name << ch
        end
      elsif ch == '['
        bracket_name = +''
        klass = self
      end
    end
  else # With no DSL available, still put this prefix into the JoinArray so we can get primary key (ID) info from this table
    x = prefix.each_with_object(build_array) { |v, s| s[v.to_sym] }
    x[prefix[-1]] = nil unless prefix.empty? # Using []= will "hydrate" any missing part(s) in our whole series
  end
  members
end

.is_view?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/brick/extensions.rb', line 66

def self.is_view?
  false
end

Instance Method Details

#brick_descripObject

If available, parse simple DSL attached to a model in order to provide a friendlier name. Object property names can be referenced in square brackets like this: { ‘User’ => ‘[profile.firstname] [profile.lastname]’ }



123
124
125
# File 'lib/brick/extensions.rb', line 123

def brick_descrip
  self.class.brick_descrip(self)
end