Class: ActiveRecord::Base
Class Attribute Summary collapse
-
._brick_relation ⇒ Object
readonly
Returns the value of attribute _brick_relation.
Class Method Summary collapse
- ._active_storage_name(col_name) ⇒ Object
-
._activestorage_actiontext_fields ⇒ Object
Return three lists of fields for this model -- has_one_attached, has_many_attached, and has_rich_text.
- ._assoc_names ⇒ Object
-
._br_associatives ⇒ Object
has_many :through associative tables.
-
._br_bt_descrip ⇒ Object
belongs_to DSL descriptions.
-
._br_cust_cols ⇒ Object
Custom columns.
-
._br_hm_counts ⇒ Object
has_many count definitions.
- ._br_quoted_name(name) ⇒ Object
- ._brick_all_fields(skip_id = nil) ⇒ Object
-
._brick_calculate_bts_hms(translations, join_array) ⇒ Object
Search for custom column, BT, HM, and HMT DSL stuff.
- ._brick_calculate_ordering(ordering, is_do_txt = true) ⇒ Object
- ._brick_deserialized_id(obj, id_col) ⇒ Object
- ._brick_deserialized_value(obj, descrip_cols) ⇒ Object
- ._brick_find_permits(model_or_assoc, current_permits, is_for_expects = nil, done_permits = []) ⇒ Object
- ._brick_get_fks ⇒ Object
-
._brick_index(mode = nil, separator = nil, relation = nil, not_path = nil) ⇒ Object
Providing a relation object allows auto-modules built from table name prefixes to work.
- ._brick_inheriteds ⇒ Object
- ._brick_monetized_attributes ⇒ Object
- ._brick_primary_key(relation = nil) ⇒ Object
- ._brick_set_wheres(params, join_array = nil) ⇒ Object
- ._pk_as_array ⇒ Object
- .brick_descrip(obj, data = nil, pk_alias = nil) ⇒ Object
- .brick_foreign_type(assoc) ⇒ Object
-
.brick_get_dsl ⇒ Object
Used to show a little prettier name for an object.
- .brick_group(*args, withhold_ids: true, **kwargs) ⇒ Object
- .brick_import_template ⇒ Object
- .brick_parse_dsl(join_array = nil, prefix = [], translations = {}, is_polymorphic = false, dsl = nil, emit_dsl = false) ⇒ Object
- .brick_pluck(*args, withhold_ids: true, **kwargs) ⇒ Object
- .brick_select(*args, **kwargs) ⇒ Object
- .brick_where(*args) ⇒ Object
- .bt_link(assoc_name) ⇒ Object
-
.find_real_model(params) ⇒ Object
Accommodate STI.
-
.inherited(model) ⇒ Object
Track the file(s) in which each model is defined.
- .is_brick? ⇒ Boolean
- .is_mssql ⇒ Object
- .is_mysql ⇒ Object
- .is_postgres ⇒ Object
- .is_view? ⇒ Boolean
- .json_column?(col) ⇒ Boolean
- .real_singular(params) ⇒ Object
Instance Method Summary collapse
-
#brick_descrip(data = nil, pk_alias = nil) ⇒ Object
If available, parse simple DSL attached to a model in order to provide a friendlier name.
Class Attribute Details
._brick_relation ⇒ Object (readonly)
Returns the value of attribute _brick_relation.
49 50 51 |
# File 'lib/brick/extensions.rb', line 49 def _brick_relation @_brick_relation end |
Class Method Details
._active_storage_name(col_name) ⇒ Object
163 164 165 166 167 168 169 170 171 |
# File 'lib/brick/extensions.rb', line 163 def _active_storage_name(col_name) if Object.const_defined?('ActiveStorage') && (self <= ::ActiveStorage::Attachment || self <= ::ActiveStorage::Blob) if (col_str = col_name.to_s).end_with?('_attachments') col_str[0..-13] elsif col_str.end_with?('_blobs') col_str[0..-7] end end end |
._activestorage_actiontext_fields ⇒ Object
Return three lists of fields for this model -- has_one_attached, has_many_attached, and has_rich_text
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/brick/extensions.rb', line 145 def _activestorage_actiontext_fields fields = [[], [], {}] if Object.const_defined?('ActiveStorage') && respond_to?(:generated_association_methods) && !(self <= ::ActiveStorage::Blob) # ActiveStorage generated_association_methods.instance_methods.each do |method_sym| method_str = method_sym.to_s fields[0] << method_str[0..-13].to_sym if method_str.end_with?('_attachment=') # has_one_attached fields[1] << method_str[0..-14].to_sym if method_str.end_with?('_attachments=') # has_many_attached end end if respond_to?(:rich_text_association_names) # ActionText rich_text_association_names&.each do |rtan| # has_rich_text rtan_str = rtan.to_s fields[2][rtan] = rtan_str.start_with?('rich_text_') ? rtan_str[10..-1].to_sym : rtan end end fields end |
._assoc_names ⇒ Object
65 66 67 |
# File 'lib/brick/extensions.rb', line 65 def _assoc_names @_assoc_names ||= {} end |
._br_associatives ⇒ Object
has_many :through associative tables
476 477 478 |
# File 'lib/brick/extensions.rb', line 476 def _br_associatives @_br_associatives ||= {} end |
._br_bt_descrip ⇒ Object
belongs_to DSL descriptions
468 469 470 |
# File 'lib/brick/extensions.rb', line 468 def _br_bt_descrip @_br_bt_descrip ||= {} end |
._br_cust_cols ⇒ Object
Custom columns
480 481 482 |
# File 'lib/brick/extensions.rb', line 480 def _br_cust_cols @_br_cust_cols ||= {} end |
._br_hm_counts ⇒ Object
has_many count definitions
472 473 474 |
# File 'lib/brick/extensions.rb', line 472 def _br_hm_counts @_br_hm_counts ||= {} end |
._br_quoted_name(name) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/brick/extensions.rb', line 202 def _br_quoted_name(name) name = name.join('.') if name.is_a?(Array) if name == '*' name elsif is_mysql "`#{name.gsub('.', '`.`')}`" elsif is_postgres || is_mssql "\"#{name.gsub('.', '"."')}\"" else name end end |
._brick_all_fields(skip_id = nil) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/brick/extensions.rb', line 104 def _brick_all_fields(skip_id = nil) col_names = columns_hash.keys # If it's a composite primary key then allow all the values through # TODO: Should disallow any autoincrement / SERIAL columns if skip_id && (pk_as_array = _pk_as_array).length == 1 && [:string, :text].exclude?(columns_hash[pk_as_array.first]&.type) col_names -= pk_as_array end hoa, hma, rtans = _activestorage_actiontext_fields col_names.map(&:to_sym) + hoa + hma.map { |as| { as => [] } } + rtans.values end |
._brick_calculate_bts_hms(translations, join_array) ⇒ Object
Search for custom column, BT, HM, and HMT DSL stuff
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 |
# File 'lib/brick/extensions.rb', line 509 def self._brick_calculate_bts_hms(translations, join_array) # Add any custom columns ::Brick.config.custom_columns&.fetch(table_name, nil)&.each do |k, cc| if cc.is_a?(Array) fk_col = cc.last unless cc.last.blank? cc = cc.first else fk_col = true end # false = not polymorphic, and true = yes -- please emit_dsl pieces, my_dsl = brick_parse_dsl(join_array, [], translations, false, cc, true) _br_cust_cols[k] = [pieces, my_dsl, fk_col] end bts, hms, associatives = ::Brick.get_bts_and_hms(self, true) bts.each do |_k, bt| next if bt[2] # Polymorphic? # join_array will receive this relation name when calling #brick_parse_dsl _br_bt_descrip[bt.first] = if bt[1].is_a?(Array) # Last param here: "true" is for yes, we are polymorphic bt[1].each_with_object({}) { |bt_class, s| s[bt_class] = bt_class.brick_parse_dsl(join_array, bt.first, translations, true) } else { bt.last => bt[1].brick_parse_dsl(join_array, bt.first, translations) } end end skip_klass_hms = ::Brick.config.skip_index_hms[self.name] || {} hms.each do |k, hm| next if skip_klass_hms.key?(k) || !hm.klass.table_exists? if hm.macro == :has_one # For our purposes a :has_one is similar enough to a :belongs_to that we can just join forces _br_bt_descrip[k] = { hm.klass => hm.klass.brick_parse_dsl(join_array, k, translations) } else # Standard :has_many _br_hm_counts[k] = hm unless hm.[:through] && !_br_associatives.fetch(hm.name, nil) end end end |
._brick_calculate_ordering(ordering, is_do_txt = true) ⇒ Object
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 |
# File 'lib/brick/extensions.rb', line 547 def self._brick_calculate_ordering(ordering, is_do_txt = true) order_by_txt = [] if is_do_txt ordering = [ordering] if ordering && !ordering.is_a?(Array) order_by = ordering&.each_with_object([]) do |ord_part, s| # %%% If a term is also used as an eqi-condition in the WHERE clause, it can be omitted from ORDER BY case ord_part when String ord_expr = if ord_part.index('(') # Any kind of SQL function at play here? ord_part.gsub('^^^', _br_quoted_name(table_name)) else _br_quoted_name(ord_part.gsub('^^^', table_name)) end s << Arel.sql(ord_expr) order_by_txt&.<<(ord_expr.index('.') ? "Arel.sql(#{ord_expr.inspect})" : ord_part.inspect) else # Expecting only Symbol ord_part = ord_part.to_s if ord_part[0] == '-' # First char '-' means descending order ord_part.slice!(0) is_desc = true end if ord_part[0] == '~' # Char '~' means order NULLs as highest values instead of lowest ord_part.slice!(0) # (Unfortunately SQLServer does not support NULLS FIRST / NULLS LAST, so leave them out.) is_nulls_switch = if is_mysql :mysql else # PostgreSQL, OracleEnhanced, SQLite :pg end end if _br_hm_counts.key?(ord_part_sym = ord_part.to_sym) ord_part = _br_quoted_name("b_r_#{ord_part}_ct") elsif _br_bt_descrip.key?(ord_part_sym) ord_part = _br_bt_descrip.fetch(ord_part_sym, nil)&.first&.last&.first&.last&.dup elsif !_br_cust_cols.key?(ord_part_sym) && !column_names.include?(ord_part) # Disallow ordering by a bogus column # %%% Note this bogus entry so that Javascript can remove any bogus _brick_order # parameter from the querystring, pushing it into the browser history. ord_part = nil end if ord_part ord_part << ' DESC' if is_desc ord_part << (is_desc ? ' NULLS LAST' : ' NULLS FIRST') if is_nulls_switch == :pg ord_part.insert(0, '-') if is_nulls_switch == :mysql order_by_txt&.<<("Arel.sql(#{ord_part.inspect})") # # Retain any reference to a bt_descrip as being a symbol # # Was: "#{quoted_table_name}.\"#{ord_part}\"" # order_by_txt&.<<(_br_bt_descrip.key?(ord_part) ? ord_part : ord_part.inspect) s << ord_part end end end [order_by, order_by_txt] end |
._brick_deserialized_id(obj, id_col) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/brick/extensions.rb', line 190 def _brick_deserialized_id(obj, id_col) id = [] self._pk_as_array.each_with_index do |pk_part, idx| id << if (id_sym = id_col[idx]&.to_sym) && obj.respond_to?(id_sym) self._brick_deserialized_value(obj, [[pk_part, id_col[idx]]])&.first else id_col[idx] end end id end |
._brick_deserialized_value(obj, descrip_cols) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/brick/extensions.rb', line 177 def _brick_deserialized_value(obj, descrip_cols) descrip_cols.map do |descrip_part| # 0..62 because Postgres column names are limited to 63 characters if (val = obj.send(descrip_part[-1][0..62])) && ActiveRecord::ConnectionAdapters::Column.instance_methods.include?(:cast_type) cast_type = self.columns_hash[descrip_part[-2]]&.send(:cast_type) val = cast_type.deserialize(val) val = val.force_encoding('UTF-8') if val.is_a?(String) end val end end |
._brick_find_permits(model_or_assoc, current_permits, is_for_expects = nil, done_permits = []) ⇒ Object
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 |
# File 'lib/brick/extensions.rb', line 484 def _brick_find_permits(model_or_assoc, current_permits, is_for_expects = nil, done_permits = []) unless done_permits.include?(model_or_assoc) done_permits << model_or_assoc self.reflect_on_all_associations.select { |assoc| !assoc.belongs_to? }.each_with_object([]) do |assoc, s| if assoc.[:through] current_permits << { "#{assoc.name.to_s.singularize}_ids".to_sym => [] } s << "#{assoc.name.to_s.singularize}_ids: []" end if self.instance_methods.include?(:"#{assoc.name}_attributes=") # Support nested attributes which use the friendly_id gem assoc.klass._brick_nested_friendly_id if Object.const_defined?('FriendlyId') && assoc.klass.instance_variable_get(:@friendly_id_config) new_attrib_text = assoc.klass._brick_find_permits(assoc, (new_permits = assoc.klass._brick_all_fields), done_permits) new_permits << :_destroy new_permits = [new_permits] if is_for_expects current_permits << { "#{assoc.name}_attributes".to_sym => new_permits } s << "#{assoc.name}_attributes: #{new_attrib_text}" end end end current_permits end |
._brick_get_fks ⇒ Object
621 622 623 624 625 626 |
# File 'lib/brick/extensions.rb', line 621 def self._brick_get_fks @_brick_get_fks ||= reflect_on_all_associations.select { |a2| a2.macro == :belongs_to }.each_with_object([]) do |bt, s| s << bt.foreign_key s << bt.foreign_type if bt.polymorphic? end end |
._brick_index(mode = nil, separator = nil, relation = nil, not_path = nil) ⇒ Object
Providing a relation object allows auto-modules built from table name prefixes to work
450 451 452 453 454 |
# File 'lib/brick/extensions.rb', line 450 def self._brick_index(mode = nil, separator = nil, relation = nil, not_path = nil) return if abstract_class? || !table_exists? ::Brick._brick_index(table_name, mode, separator, relation, not_path) end |
._brick_inheriteds ⇒ Object
51 52 53 |
# File 'lib/brick/extensions.rb', line 51 def _brick_inheriteds @_brick_inheriteds ||= ::ActiveRecord::Base.instance_variable_get(:@_brick_inheriteds) end |
._brick_monetized_attributes ⇒ Object
270 271 272 |
# File 'lib/brick/extensions.rb', line 270 def _brick_monetized_attributes @_brick_monetized_attributes ||= respond_to?(:monetized_attributes) ? monetized_attributes.values : {} end |
._brick_primary_key(relation = nil) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/brick/extensions.rb', line 225 def _brick_primary_key(relation = nil) return @_brick_primary_key if instance_variable_defined?(:@_brick_primary_key) pk = begin primary_key rescue superclass.respond_to?(:primary_key) && superclass.primary_key end pk = pk.is_a?(String) ? [pk] : pk.dup || [] pk.map! { |pk_part| pk_part =~ /^[A-Z0-9_]+$/ ? pk_part.downcase : pk_part } unless connection.adapter_name == 'MySQL2' # Just return [] if we're missing any part of the primary key. (PK is usually just "id") if relation && pk.present? @_brick_primary_key ||= pk.any? { |pk_part| !relation[:cols].key?(pk_part) } ? [] : pk else # No definitive key yet, so return what we can without setting the instance variable pk end end |
._brick_set_wheres(params, join_array = nil) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/brick/extensions.rb', line 116 def _brick_set_wheres(params, join_array = nil) is_distinct = nil wheres = {} params.each do |k, v| k = k.to_s # Rails < 4.2 comes in as a symbol next unless k.start_with?('__') k = k[2..-1] # Take off leading "__" if (where_col = (ks = k.split('.')).last)[-1] == '!' where_col = where_col[0..-2] end case ks.length when 1 next unless self.column_names.any?(where_col) || self._brick_get_fks.include?(where_col) when 2 assoc_name = ks.first.to_sym # Make sure it's a good association name and that the model has that column name next unless self.reflect_on_association(assoc_name)&.klass&.column_names&.any?(where_col) join_array&.[]=(assoc_name, nil) # Store this relation name in our special collection for .joins() is_distinct = true end wheres[k] = v.is_a?(String) ? v.split(',') : v end [wheres, is_distinct] end |
._pk_as_array ⇒ Object
173 174 175 |
# File 'lib/brick/extensions.rb', line 173 def _pk_as_array self.primary_key.is_a?(Array) ? self.primary_key : [self.primary_key] end |
.brick_descrip(obj, data = nil, pk_alias = nil) ⇒ Object
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
# File 'lib/brick/extensions.rb', line 365 def self.brick_descrip(obj, data = nil, pk_alias = nil) dsl = obj if obj.is_a?(String) 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 = begin caches.fetch(obj_name) { caches[obj_name] = this_obj&.send(part.to_sym) } rescue clsnm = part.camelize if (possible = this_obj.class.reflect_on_all_associations.select { |a| !a.polymorphic? && (a.class_name == clsnm || a.klass.base_class.name == clsnm) }.first) caches[obj_name] = this_obj&.send(possible.name) end end break if this_obj.nil? end if this_obj.is_a?(ActiveRecord::Base) && (obj_descrip = this_obj.class.brick_descrip(this_obj)) this_obj = obj_descrip end if Object.const_defined?('ActiveStorage') && this_obj.is_a?(::ActiveStorage::Filename) && this_obj.instance_variable_get(:@filename).nil? this_obj.instance_variable_set(:@filename, '') end this_obj&.to_s || '' end begin is_brackets_have_content = true unless datum.blank? rescue end 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 ||= primary_key) pk_alias = [pk_alias] unless pk_alias.is_a?(Array) id = [] pk_alias.each do |pk_alias_part| if (pk_part = obj.respond_to?(pk_alias_part) ? obj.send(pk_alias_part) : nil) id << pk_part end end if id.present? "#{klass.name} ##{id.join(', ')}" end else obj.to_s end end |
.brick_foreign_type(assoc) ⇒ Object
100 101 102 |
# File 'lib/brick/extensions.rb', line 100 def brick_foreign_type(assoc) reflect_on_association(assoc).foreign_type || "#{assoc}_type" end |
.brick_get_dsl ⇒ Object
Used to show a little prettier name for an object
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/brick/extensions.rb', line 244 def brick_get_dsl if ::Brick.config.model_descrips.key?(name) ::Brick.config.model_descrips[name] else # If there's no DSL yet specified, just try to find the first usable column on this model dsl = if table_exists? skip_columns = _brick_get_fks + (::Brick.config. || []) column_list = columns.dup pk_idx = column_list.index { |c| c.name == primary_key } if pk_idx pk_column = column_list.delete_at(pk_idx) # if the PK column is a string, put it at the end so it will get chosen as a last resort column_list << pk_column if [:string, :text].include?(columns_hash[primary_key]&.type) end if (descrip_col = column_list.find { |c| c.type == :string && skip_columns.exclude?(c.name) }) "[#{descrip_col.name}]" elsif (descrip_col = column_list.find { |c| [:boolean, :binary, :xml].exclude?(c.type) && skip_columns.exclude?(c.name) }) "[#{descrip_col.name}]" else "#{name} ##{_pk_as_array.map { |pk_part| "[#{pk_part}]" }.join(', ')}" end end ::Brick.config.model_descrips[name] = dsl end end |
.brick_group(*args, withhold_ids: true, **kwargs) ⇒ Object
615 616 617 |
# File 'lib/brick/extensions.rb', line 615 def self.brick_group(*args, withhold_ids: true, **kwargs) all.brick_group(*args, withhold_ids: withhold_ids, **kwargs) end |
.brick_import_template ⇒ Object
456 457 458 459 460 461 462 463 464 |
# File 'lib/brick/extensions.rb', line 456 def self.brick_import_template template = constants.include?(:IMPORT_TEMPLATE) ? self::IMPORT_TEMPLATE : suggest_template(0, false, true) # Add the primary key to the template as being unique (unless it's already there) if primary_key template[:uniques] = [pk = primary_key.to_sym] template[:all].unshift(pk) unless template[:all].include?(pk) end template end |
.brick_parse_dsl(join_array = nil, prefix = [], translations = {}, is_polymorphic = false, dsl = nil, emit_dsl = false) ⇒ Object
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/brick/extensions.rb', line 275 def self.brick_parse_dsl(join_array = nil, prefix = [], translations = {}, is_polymorphic = false, dsl = nil, emit_dsl = false) unless join_array.is_a?(::Brick::JoinArray) join_array = ::Brick::JoinArray.new.tap { |ary| ary.replace([join_array]) } if join_array.is_a?(::Brick::JoinHash) join_array = ::Brick::JoinArray.new unless join_array.nil? || join_array.is_a?(Array) end prefix = [prefix] unless prefix.is_a?(Array) members = [] unless dsl || (dsl = ::Brick.config.model_descrips[name] || brick_get_dsl) # 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(join_array) { |v, s| s[v.to_sym] } x[prefix.last] = nil unless prefix.empty? # Using []= will "hydrate" any missing part(s) in our whole series return members end # Do the actual dirty work of recursing through nested DSL bracket_name = nil dsl2 = +'' # To replace our own DSL definition in case it needs to be expanded dsl3 = +'' # To return expanded DSL that is nested from another model 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].each_with_object([]) do |part, s| unless (klass = (orig_class = klass).reflect_on_association(part_sym = part.to_sym)&.klass) puts "Couldn't reference #{orig_class.name}##{part} that's part of the DSL \"#{dsl}\"." break end s << part_sym end if first_parts if (parts = prefix + first_parts + [parts[-1]]).length > 1 && klass join_array.add_parts(parts) unless is_polymorphic translations[parts[0..-2].join('.')] = klass end if klass&.column_names.exclude?(parts.last) && (klass = (orig_class = klass).reflect_on_association(possible_dsl = parts.last&.to_sym)&.klass) parts.pop if prefix.empty? # Custom columns start with an empty prefix prefix << parts.shift until parts.empty? end # Expand this entry which refers to an association name members2, dsl2a = klass.brick_parse_dsl(join_array, prefix + [possible_dsl], translations, is_polymorphic, nil, true) members += members2 dsl2 << dsl2a dsl3 << dsl2a else dsl2 << "[#{bracket_name}]" if emit_dsl dsl3 << "[#{prefix[1..-1].map { |p| "#{p.to_s}." }.join if prefix.length > 1}#{bracket_name}]" end parts[-1] = column_names.first if parts[-1].nil? # No primary key to be found? Grab something to display! members << parts end end bracket_name = nil else bracket_name << ch end elsif ch == '[' bracket_name = +'' klass = self else dsl2 << ch dsl3 << ch end end # Rewrite the DSL in case it's now different from having to expand it # if ::Brick.config.model_descrips[name] != dsl2 # puts ::Brick.config.model_descrips[name] # puts dsl2.inspect # puts dsl3.inspect # binding.pry # end if emit_dsl # Had been: [members, dsl2, dsl3] [members, dsl3] else ::Brick.config.model_descrips[name] = dsl2 members end end |
.brick_pluck(*args, withhold_ids: true, **kwargs) ⇒ Object
607 608 609 |
# File 'lib/brick/extensions.rb', line 607 def self.brick_pluck(*args, withhold_ids: true, **kwargs) all.brick_pluck(*args, withhold_ids: withhold_ids, **kwargs) end |
.brick_select(*args, **kwargs) ⇒ Object
603 604 605 |
# File 'lib/brick/extensions.rb', line 603 def self.brick_select(*args, **kwargs) all.brick_select(*args, **kwargs) end |
.brick_where(*args) ⇒ Object
611 612 613 |
# File 'lib/brick/extensions.rb', line 611 def self.brick_where(*args) all.brick_where(*args) end |
.bt_link(assoc_name) ⇒ Object
437 438 439 440 441 442 443 444 445 446 447 |
# File 'lib/brick/extensions.rb', line 437 def self.bt_link(assoc_name) assoc_html_name = unless (assoc_name = assoc_name.to_s).camelize == name CGI.escapeHTML(assoc_name) end model_path = ::Rails.application.routes.url_helpers.send("#{_brick_index || table_name}_path".to_sym) model_path << "?#{self.inheritance_column}=#{self.name}" if self != base_class av_class = Class.new.extend(ActionView::Helpers::UrlHelper) av_class.extend(ActionView::Helpers::TagHelper) if ActionView.version < ::Gem::Version.new('7') link = av_class.link_to(assoc_html_name ? name : assoc_name, model_path) assoc_html_name ? "#{assoc_name}-#{link}".html_safe : link end |
.find_real_model(params) ⇒ Object
Accommodate STI
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/brick/extensions.rb', line 74 def find_real_model(params) if params && ((sub_name = params.fetch(inheritance_column, nil)).present? || (sub_name = params[name.underscore]&.fetch(inheritance_column, nil))) sub_name = sub_name.first if sub_name.is_a?(Array) # Support the params style that gets returned from #_brick_querying # Make sure the chosen model is really the same or a subclass of this model return self if sub_name.blank? (possible_model = sub_name.constantize) <= self ? possible_model : self else self end end |
.inherited(model) ⇒ Object
Track the file(s) in which each model is defined
56 57 58 59 |
# File 'lib/brick/extensions.rb', line 56 def inherited(model) (_brick_inheriteds[model] ||= []) << caller.first.split(':')[0..1] unless caller.first.include?('/lib/brick/extensions.rb:') super end |
.is_brick? ⇒ Boolean
61 62 63 |
# File 'lib/brick/extensions.rb', line 61 def is_brick? instance_variables.include?(:@_brick_relation) && instance_variable_get(:@_brick_relation) end |
.is_mssql ⇒ Object
221 222 223 |
# File 'lib/brick/extensions.rb', line 221 def is_mssql @is_mssql ||= connection.adapter_name == 'SQLServer' end |
.is_mysql ⇒ Object
218 219 220 |
# File 'lib/brick/extensions.rb', line 218 def is_mysql @is_mysql ||= ['Mysql2', 'Trilogy'].include?(connection.adapter_name) end |
.is_postgres ⇒ Object
215 216 217 |
# File 'lib/brick/extensions.rb', line 215 def is_postgres @is_postgres ||= connection.adapter_name == 'PostgreSQL' end |
.is_view? ⇒ Boolean
69 70 71 |
# File 'lib/brick/extensions.rb', line 69 def is_view? false end |
.json_column?(col) ⇒ Boolean
92 93 94 95 96 97 98 |
# File 'lib/brick/extensions.rb', line 92 def json_column?(col) col.type == :json || ::Brick.config.json_columns[table_name]&.include?(col.name) || ( respond_to?(:attribute_types) && (attr_types = attribute_types[col.name]).respond_to?(:coder) && (attr_types.coder.is_a?(Class) ? attr_types.coder : attr_types.coder&.class)&.name&.end_with?('JSON') ) end |
.real_singular(params) ⇒ Object
87 88 89 90 |
# File 'lib/brick/extensions.rb', line 87 def real_singular(params) real_model = find_real_model(params) [real_model, real_model.name.underscore.tr('/', '_')] end |
Instance Method Details
#brick_descrip(data = nil, pk_alias = nil) ⇒ Object
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]' }
361 362 363 |
# File 'lib/brick/extensions.rb', line 361 def brick_descrip(data = nil, pk_alias = nil) self.class.brick_descrip(self, data, pk_alias) end |