Module: Brick::Rails::FormTags
- Included in:
- ActionView::Helpers::FormTagHelper
- Defined in:
- lib/brick/frameworks/rails/form_tags.rb
Instance Method Summary collapse
-
#brick_grid(relation = nil, bt_descrip = nil, sequence = nil, inclusions = nil, exclusions = nil, cols = {}, poly_cols = nil, bts = {}, hms_keys = [], hms_cols = {}) ⇒ Object
Our super speedy grid.
-
#link_to_brick(*args, **kwargs) ⇒ Object
brick_grid.
Instance Method Details
#brick_grid(relation = nil, bt_descrip = nil, sequence = nil, inclusions = nil, exclusions = nil, cols = {}, poly_cols = nil, bts = {}, hms_keys = [], hms_cols = {}) ⇒ Object
Our super speedy grid
3 4 5 6 7 8 9 10 11 12 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 71 72 73 74 75 76 77 78 79 80 81 82 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 119 120 121 122 123 124 125 126 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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/brick/frameworks/rails/form_tags.rb', line 3 def brick_grid(relation = nil, bt_descrip = nil, sequence = nil, inclusions = nil, exclusions = nil, cols = {}, poly_cols = nil, bts = {}, hms_keys = [], hms_cols = {}) # When a relation is not provided, first see if one exists which matches the controller name unless (relation ||= instance_variable_get("@#{controller_name}".to_sym)) # Failing that, dig through the instance variables with hopes to find something that is an ActiveRecord::Relation case (collections = _brick_resource_from_iv).length when 0 puts '#brick_grid: Not having been provided with a collection to work from, searched through all instance variables to find an ActiveRecord::Relation. None could be found.' return when 1 # If there's only one type match then simply get the first one, hoping that this is what they intended relation = instance_variable_get(iv = (chosen = collections.first).last.first) puts "#brick_grid: Not having been provided with a collection to work from, first tried @#{controller_name}. Failing that, have searched through instance variables and found #{iv} of type #{chosen.first.name}. Running with it!" else myriad = collections.each_with_object([]) { |c, s| c.last.each { |iv| s << "#{iv} (#{c.first.name})" } } puts "#brick_grid: Not having been provided with a collection to work from, first tried @#{controller_name}, and then searched through all instance variables. Found ActiveRecord::Relation objects of multiple types: #{myriad.inspect} Not knowing which of these to render, have erred on the side of caution and simply provided this warning message." return end end nfc = Brick.config.sidescroll.fetch(relation.table_name, nil)&.fetch(:num_frozen_columns, nil) || Brick.config.sidescroll.fetch(:num_frozen_columns, nil) || 0 out = "<table id=\"headerTop\"></table> <table id=\"#{relation.table_name.split('.').last}\" class=\"shadow\"#{ " x-num-frozen=\"#{nfc}\"" if nfc.positive? }> <thead><tr>" pk = (klass = relation.klass).primary_key || [] pk = [pk] unless pk.is_a?(Array) if pk.present? out << "<th x-order=\"#{pk.join(',')}\"></th>" end col_keys = relation.columns.each_with_object([]) do |col, s| col_name = col.name next if inclusions&.exclude?(col_name) || (pk.include?(col_name) && [:integer, :uuid].include?(col.type) && !bts&.key?(col_name)) || ::Brick.config..include?(col_name) || poly_cols&.include?(col_name) s << col_name cols[col_name] = col end composite_bts = bts.select { |k, _v| k.is_a?(Array) } composite_bt_names = {} composite_bt_cols = composite_bts.each_with_object([]) do |bt, s| composite_bt_names[bt.first.join('__')] = bt.last bt.first.each { |bt_col| s << bt_col unless s.include?(bt_col.first) } end unless sequence # If no sequence is defined, start with all inclusions cust_cols = klass._br_cust_cols # HOT columns, kept as symbols hots = klass._br_bt_descrip.keys.select { |k| bts.key?(k) } sequence = (col_keys - composite_bt_cols) + composite_bt_names.keys + cust_cols.keys + hots + hms_keys.reject { |assoc_name| inclusions&.exclude?(assoc_name) } end sequence.reject! { |nm| exclusions.include?(nm) } if exclusions out << sequence.each_with_object(+'') do |col_name, s| s << '<th ' if (col = cols[col_name]).is_a?(ActiveRecord::ConnectionAdapters::Column) s << "title=\"#{col.comment}\" " if col.respond_to?(:comment) && !col.comment.blank? s << if (bt = bts[col_name]) # Allow sorting for any BT except polymorphics "x-order=\"#{bt.first.to_s + '"' unless bt[2]}>BT " + bt[1].map { |bt_pair| bt_pair.first.bt_link(bt.first) }.join(' ') else # Normal column col_name_humanised = klass.human_attribute_name(col_name, { default: col_name }) "x-order=\"#{col_name + '"' if true}>#{col_name_humanised}" end elsif col # HM column = {} [col[1].inheritance_column] = col[1].name unless col[1] == col[1].base_class s << "x-order=\"#{col_name + '"' if true}>#{col[2]} " s << (col.first ? "#{col[3]}" : "#{link_to(col[3], send("#{col[1]._brick_index}_path", ))}") elsif cust_cols.key?(col_name) # Custom column s << "x-order=\"#{col_name}\">#{col_name}" elsif col_name.is_a?(Symbol) && (hot = bts[col_name]) # has_one :through s << "x-order=\"#{hot.first.to_s}\">HOT " + hot[1].map { |hot_pair| hot_pair.first.bt_link(col_name) }.join(' ') elsif (bt = composite_bt_names[col_name]) s << "x-order=\"#{bt.first.to_s + '"' unless bt[2]}>BT comp " + bt[1].map { |bt_pair| bt_pair.first.bt_link(bt.first) }.join(' ') else # Bad column name! s << "title=\"<< Unknown column >>\">#{col_name}" end s << '</th>' end out << "</tr></thead> <tbody>" # %%% Have once gotten this error with MSSQL referring to http://localhost:3000/warehouse/cold_room_temperatures__archive # ActiveRecord::StatementTimeout in Warehouse::ColdRoomTemperatures_Archive#index # TinyTds::Error: Adaptive Server connection timed out # (After restarting the server it worked fine again.) relation.each do |obj| out << "<tr>\n" out << "<td class=\"col-sticky\">#{link_to('⇛', send("#{klass._brick_index(:singular)}_path".to_sym, pk.map { |pk_part| obj.send(pk_part.to_sym) }), { class: 'big-arrow' })}</td>\n" if pk.present? sequence.each_with_index do |col_name, idx| val = obj.attributes[col_name] bt = bts[col_name] out << '<td' (classes ||= []) << 'col-sticky' if idx < nfc (classes ||= []) << 'dimmed' unless cols.key?(col_name) || (cust_col = cust_cols[col_name]) || (col_name.is_a?(Symbol) && bts.key?(col_name)) # HOT (classes ||= []) << 'right' if val.is_a?(Numeric) && !bt out << " class=\"#{classes.join(' ')}\"" if classes&.present? out << '>' if (bt || composite_bt_names[col_name]) if bt[2] # Polymorphic? if (poly_id = obj.send("#{bt.first}_id")) bt_class = obj.send(klass.brick_foreign_type(bt.first)) base_class_underscored = (::Brick.existing_stis[bt_class] || bt_class).constantize.base_class._brick_index(:singular) out << link_to("#{bt_class} ##{poly_id}", send("#{base_class_underscored}_path".to_sym, poly_id)) end else # BT or HOT bt_class = bt[1].first.first if bt_descrip descrips = bt_descrip[bt.first][bt_class] bt_id_col = if descrips.nil? puts "Caught it in the act for obj / #{col_name}!" elsif descrips.length == 1 [klass.reflect_on_association(bt.first)&.foreign_key] else descrips.last end end br_descrip_args = [obj] # 0..62 because Postgres column names are limited to 63 characters br_descrip_args += [descrips[0..-2].map { |id| obj.send(id.last[0..62]) }, bt_id_col] if descrips bt_txt = bt_class.brick_descrip(*br_descrip_args) bt_txt = ::Brick::Rails.display_binary(bt_txt).html_safe if bt_txt&.encoding&.name == 'ASCII-8BIT' bt_txt ||= "<span class=\"orphan\"><< Orphaned ID: #{val} >></span>" if val bt_id = bt_id_col&.map { |id_col| obj.respond_to?(id_sym = id_col.to_sym) ? obj.send(id_sym) : id_col } out << (bt_id&.first ? link_to(bt_txt, send("#{bt_class.base_class._brick_index(:singular)}_path".to_sym, bt_id)) : bt_txt || '') end elsif (hms_col = hms_cols[col_name]) if hms_col.length == 1 out << hms_col.first else hm_klass = (col = cols[col_name])[1] if col[2] == 'HO' descrips = bt_descrip[col_name.to_sym][hm_klass] if (ho_id = (ho_id_col = descrips.last).map { |id_col| obj.send(id_col.to_sym) })&.first ho_txt = if hm_klass.name == 'ActiveStorage::Attachment' begin ::Brick::Rails.display_binary(obj.send(col[3])&.blob&.download)&.html_safe rescue end else hm_klass.brick_descrip(obj, descrips[0..-2].map { |id| obj.send(id.last[0..62]) }, ho_id_col) end out << link_to(ho_txt, send("#{hm_klass.base_class._brick_index(:singular)}_path".to_sym, ho_id)) end elsif obj.respond_to?(ct_col = hms_col[1].to_sym) && (ct = obj.send(ct_col)&.to_i)&.positive? predicates = hms_col[2].each_with_object({}) { |v, s| s[v.first] = v.last.is_a?(String) ? v.last : obj.send(v.last) } predicates.each { |k, v| predicates[k] = klass.name if v == '[sti_type]' } out << "#{link_to("#{ct || 'View'} #{hms_col.first}", send("#{hm_klass._brick_index}_path".to_sym, predicates))}\n" end end elsif (col = cols[col_name]).is_a?(ActiveRecord::ConnectionAdapters::Column) # binding.pry if col.is_a?(Array) out << if @_brick_monetized_attributes&.include?(col_name) val ? Money.new(val.to_i).format : '' else lat_lng = if [:float, :decimal].include?(col.type) && ( ((col_name == 'latitude' && obj.respond_to?('longitude') && (lng = obj.send('longitude')) && lng.is_a?(Numeric) && (lat = val)) || (col_name == 'longitude' && obj.respond_to?('latitude') && (lat = obj.send('latitude')) && lat.is_a?(Numeric) && (lng = val)) ) || ((col_name == 'lat' && obj.respond_to?('lng') && (lng = obj.send('lng')) && lng.is_a?(Numeric) && (lat = val)) || (col_name == 'lng' && obj.respond_to?('lat') && (lat = obj.send('lat')) && lat.is_a?(Numeric) && (lng = val)) ) ) [lat, lng] end col_type = col&.sql_type == 'geography' ? col.sql_type : col&.type ::Brick::Rails.display_value(col_type || col&.sql_type, val, lat_lng).to_s end elsif cust_col data = cust_col.first.map { |cc_part| obj.send(cc_part.last) } cust_txt = klass.brick_descrip(cust_col[-2], data) if (link_id = obj.send(cust_col.last[1]) if cust_col.last) out << link_to(cust_txt, send("#{cust_col.last.first._brick_index(:singular)}_path", link_id)) else out << (cust_txt || '') end else # Bad column name! out << '?' end out << '</td>' end out << '</tr>' end out << " </tbody> </table> " out.html_safe end |
#link_to_brick(*args, **kwargs) ⇒ Object
brick_grid
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/brick/frameworks/rails/form_tags.rb', line 206 def link_to_brick(*args, **kwargs) return unless ::Brick.config.mode == :on kwargs.merge!(args.pop) if args.last.is_a?(Hash) # Avoid infinite recursion if (visited = kwargs.fetch(:visited, nil)) return if visited.key?(object_id) kwargs[:visited][object_id] = nil else kwargs[:visited] = {} end text = ((args.first.is_a?(String) || args.first.is_a?(Proc)) && args.shift) || args[1] text = text.call if text.is_a?(Proc) klass_or_obj = ((args.first.is_a?(ActiveRecord::Relation) || args.first.is_a?(ActiveRecord::Base) || args.first.is_a?(Class)) && args.first) || @_brick_model # If not provided, do a best-effort to automatically determine the resource class or object filter_parts = [] rel_name = nil klass_or_obj ||= begin klass, sti_type, rel_name = ::Brick.ctrl_to_klass(controller_path) if klass type_col = klass.inheritance_column # Usually 'type' filter_parts << "#{type_col}=#{sti_type}" if sti_type && klass.column_names.include?(type_col) path_params = request.path_parameters pk = (klass.primary_key || ActiveRecord::Base.primary_key).to_sym if ((id = (path_params[pk] || path_params[:id] || path_params["#{klass.name.underscore}_id".to_sym])) && (obj = klass.find_by(pk => id))) || (['show', 'edit', 'update', 'destroy'].include?(action_name) && (obj = klass.first)) obj else # %%% If there is a HMT that refers to some ___id then try to identify an appropriate filter # %%% If there is a polymorphic association that might relate to stuff in the path_params, # try to identify an appropriate ___able_id and ___able_type filter ((klass.column_names - [pk.to_s]) & path_params.keys.map(&:to_s)).each do |path_param| next if [:controller, :action].include?(path_param) foreign_id = path_params[path_param.to_sym] # Need to convert a friendly_id slug to a real ID? if Object.const_defined?('FriendlyId') && (assoc = klass.reflect_on_all_associations.find { |a| a.belongs_to? && a.foreign_key == path_param }) && (assoc_klass = assoc.klass).instance_variable_get(:@friendly_id_config) && (new_id = assoc_klass.where(assoc_klass.friendly_id_config.query_field => foreign_id) .pluck(assoc_klass.primary_key).first) foreign_id = new_id end filter_parts << "#{path_param}=#{foreign_id}" end klass end end rescue end if klass_or_obj if klass_or_obj.is_a?(ActiveRecord::Relation) klass_or_obj.where_values_hash.each do |whr| filter_parts << "#{whr.first}=#{whr.last}" if whr.last && !whr.last.is_a?(Array) end klass_or_obj = klass_or_obj.klass type_col = klass_or_obj.inheritance_column if klass_or_obj.column_names.include?(type_col) && klass_or_obj.name != klass_or_obj.base_class.name filter_parts << "#{type_col}=#{klass_or_obj.name}" end end filter = "?#{filter_parts.join('&')}" if filter_parts.present? app_routes = Rails.application.routes # In case we're operating in another engine, reference the application since Brick routes are placed there. klass = klass_or_obj.is_a?(ActiveRecord::Base) ? klass_or_obj.class : klass_or_obj relation = ::Brick.relations.fetch(rel_name || klass.table_name, nil) if (klass_or_obj&.is_a?(Class) && klass_or_obj < ActiveRecord::Base) || (klass_or_obj&.is_a?(ActiveRecord::Base) && klass_or_obj.new_record? && (klass_or_obj = klass_or_obj.class)) path = (proc = kwargs[:index_proc]) ? proc.call(klass_or_obj, relation) : "#{app_routes.path_for(controller: klass_or_obj.base_class._brick_index(nil, '/', relation), action: :index)}#{filter}" lt_args = [text || "Index for #{klass_or_obj.name.pluralize}", path] else # If there are multiple incoming parameters then last one is probably the actual ID, and first few might be some nested tree of stuff leading up to it path = (proc = kwargs[:show_proc]) ? proc.call(klass_or_obj, relation) : "#{app_routes.path_for(controller: klass_or_obj.class.base_class._brick_index(nil, '/', relation), action: :show, id: klass_or_obj)}#{filter}" lt_args = [text || "Show this #{klass_or_obj.class.name}", path] end kwargs.delete(:visited) link_to(*lt_args, **kwargs) else # puts "Warning: link_to_brick could not find a class for \"#{controller_path}\" -- consider setting @_brick_model within that controller." # if (hits = res_names.keys & instance_variables.map { |v| v.to_s[1..-1] }).present? if (links = _brick_resource_from_iv(true)).length == 1 # If there's only one match then use any text that was supplied link_to_brick(text || links.first.last.join('/'), links.first.first, **kwargs) else links.each_with_object([]) { |v, s| s << link if link = link_to_brick(v.join('/'), v, **kwargs) }.join(' ').html_safe end end end |