brick 1.0.26 → 1.0.27

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 922a55ec392e2ea9cef11c7297e4c2e4883e918130a23e49102b5a7e2442140e
4
- data.tar.gz: 7cda213ec2e7cdccae096ef4ad8091a53a91359a1e6fabeb753cc2b579ac5c16
3
+ metadata.gz: '097fb194c2b1323cd051669f362d86492d9c768f1cbccf06c2fdc45a7f266e1e'
4
+ data.tar.gz: 72746f153d073256b8be063e90c28da85a50e11865250f2570d501ff320431b3
5
5
  SHA512:
6
- metadata.gz: 7b817257f9c2aedd47bc2471b12e13edc8b370b71920258755f0f9a7594060e065d022e8d4b2b360cdc41f2a60def02cfc406889d526d80088670bd64acd58be
7
- data.tar.gz: 1ea923301103dcc53853940770861ab5c2635f1280026e0740287274c0e9bb7d566ddcf96db0d3858467b788bc9ee4b73e7bcfc61145be1bbb43775cfb859b80
6
+ metadata.gz: 83d0c40c0af17d314d6b4cd7ab0e312bce1c8d57bdd0992da1b54fa8a3e829852c6fd800c34f3bd35f3a3306bda736dc99d14af19009926bf65971a5d4904717
7
+ data.tar.gz: c82b9c31ffe3a00e701e22a3f69c85e7b60e0b3b6d24e95df7fe3f8afb85b00ab7515e0befab7dd37e8a25924274e663127b953dab8eb64041d834aa39de4bc1
data/lib/brick/config.rb CHANGED
@@ -130,6 +130,14 @@ module Brick
130
130
  @mutex.synchronize { @schema_to_analyse = schema }
131
131
  end
132
132
 
133
+ def default_route_fallback
134
+ @mutex.synchronize { @default_route_fallback }
135
+ end
136
+
137
+ def default_route_fallback=(resource_name)
138
+ @mutex.synchronize { @default_route_fallback = resource_name }
139
+ end
140
+
133
141
  def skip_database_views
134
142
  @mutex.synchronize { @skip_database_views }
135
143
  end
@@ -385,7 +385,8 @@ module ActiveRecord
385
385
  else
386
386
  fk_col = hm.foreign_key
387
387
  poly_type = hm.inverse_of.foreign_type if hm.options.key?(:as)
388
- hm.klass.primary_key || '*'
388
+ pk = hm.klass.primary_key
389
+ (pk.is_a?(Array) ? pk.first : pk) || '*'
389
390
  end
390
391
  tbl_alias = "_br_#{hm.name}"
391
392
  pri_tbl = hm.active_record
@@ -779,11 +780,15 @@ class Object
779
780
 
780
781
  if model.primary_key
781
782
  code << " def show\n"
782
- code << (find_by_id = " @#{singular_table_name} = #{model.name}.find(params[:id].split(','))\n")
783
+ code << (find_by_id = " id = params[:id]&.split(/[\\/,_]/)
784
+ id = id.first if id.is_a?(Array) && id.length == 1
785
+ @#{singular_table_name} = #{model.name}.find(id)\n")
783
786
  code << " end\n"
784
787
  self.define_method :show do
785
788
  ::Brick.set_db_schema(params)
786
- instance_variable_set("@#{singular_table_name}".to_sym, model.find(params[:id].split(',')))
789
+ id = params[:id]&.split(/[\/,_]/)
790
+ id = id.first if id.is_a?(Array) && id.length == 1
791
+ instance_variable_set("@#{singular_table_name}".to_sym, model.find(id))
787
792
  end
788
793
  end
789
794
 
@@ -112,7 +112,7 @@ module Brick
112
112
  "<%= obj = #{obj_name}.#{hm.first}; link_to(obj.brick_descrip, obj) if obj %>\n"
113
113
  end
114
114
  elsif args.first == 'show'
115
- hm_stuff << "<%= link_to '#{assoc_name}', #{hm_assoc.klass.name.underscore.pluralize}_path({ #{path_keys(hm_assoc, hm_fk_name, "@#{obj_name}&.first&", pk)} }) %>\n"
115
+ hm_stuff << "<%= link_to '#{assoc_name}', #{hm_assoc.klass.name.underscore.pluralize}_path({ #{path_keys(hm_assoc, hm_fk_name, "@#{obj_name}", pk)} }) %>\n"
116
116
  end
117
117
  s << hm_stuff
118
118
  end
@@ -444,7 +444,7 @@ function changeout(href, param, value) {
444
444
  <p style=\"color: green\"><%= notice %></p>#{"
445
445
  <select id=\"schema\">#{schema_options}</select>" if ::Brick.db_schemas.length > 1}
446
446
  <select id=\"tbl\">#{table_options}</select>
447
- <h1>#{model_name}: <%= (obj = @#{obj_name}&.first)&.brick_descrip || controller_name %></h1>
447
+ <h1>#{model_name}: <%= (obj = @#{obj_name})&.brick_descrip || controller_name %></h1>
448
448
  <%= link_to '(See all #{obj_name.pluralize})', #{table_name}_path %>
449
449
  <% if obj %>
450
450
  <%= # path_options = [obj.#{pk}]
@@ -453,7 +453,7 @@ function changeout(href, param, value) {
453
453
  form_for(obj.becomes(#{model_name})) do |f| %>
454
454
  <table>
455
455
  <% has_fields = false
456
- @#{obj_name}.first.attributes.each do |k, val| %>
456
+ @#{obj_name}.attributes.each do |k, val| %>
457
457
  <tr>
458
458
  <%# %%% Accommodate composite keys %>
459
459
  <% next if k == '#{pk}' || ::Brick.config.metadata_columns.include?(k) %>
@@ -464,10 +464,15 @@ function changeout(href, param, value) {
464
464
  bt_name = bt[1].map { |x| x.first.name }.join('/')
465
465
  # %%% Only do this if the user has permissions to edit this bt field
466
466
  if bt[2] # Polymorphic?
467
- poly_class_name = @#{obj_name}.first.send(\"#\{bt.first\}_type\")
468
- bt_pair = bt[1].find { |pair| pair.first.name == poly_class_name }
467
+ poly_class_name = @#{obj_name}.send(\"#\{bt.first\}_type\")
468
+ bt_pair = nil
469
+ loop do
470
+ bt_pair = bt[1].find { |pair| pair.first.name == poly_class_name }
471
+ # Acxommodate any valid STI by going up the chain of inheritance
472
+ break unless bt_pair.nil? && poly_class_name = ::Brick.existing_stis[poly_class_name]
473
+ end
469
474
  # descrips = @_brick_bt_descrip[bt.first][bt_class]
470
- poly_id = @#{obj_name}.first.send(\"#\{bt.first\}_id\")
475
+ poly_id = @#{obj_name}.send(\"#\{bt.first\}_id\")
471
476
  # bt_class.order(obj_pk = bt_class.primary_key).each { |obj| option_detail << [obj.brick_descrip(nil, obj_pk), obj.send(obj_pk)] }
472
477
  else # No polymorphism, so just get the first one
473
478
  bt_pair = bt[1].first
@@ -520,16 +525,17 @@ function changeout(href, param, value) {
520
525
 
521
526
  #{hms_headers.each_with_object(+'') do |hm, s|
522
527
  if (pk = hm.first.klass.primary_key)
523
- s << "<table id=\"#{hm_name = hm.first.name.to_s}\">
528
+ hm_singular_name = (hm_name = hm.first.name.to_s).singularize.underscore
529
+ obj_pk = (pk.is_a?(Array) ? pk : [pk]).each_with_object([]) { |pk_part, s| s << "#{hm_singular_name}.#{pk_part}" }.join(', ')
530
+ s << "<table id=\"#{hm_name}\">
524
531
  <tr><th>#{hm[3]}</th></tr>
525
- <% collection = @#{obj_name}.first.#{hm_name}
532
+ <% collection = @#{obj_name}.#{hm_name}
526
533
  collection = collection.is_a?(ActiveRecord::Associations::CollectionProxy) ? collection.order(#{pk.inspect}) : [collection]
527
534
  if collection.empty? %>
528
535
  <tr><td>(none)</td></tr>
529
536
  <% else %>
530
- <% collection.uniq.each do |#{hm_singular_name = hm_name.singularize.underscore}| %>
531
- <%# %%% accommodate composite primary key %>
532
- <tr><td><%= link_to(#{hm_singular_name}.brick_descrip, #{hm.first.klass.name.underscore}_path(#{hm_singular_name}.#{pk})) %></td></tr>
537
+ <% collection.uniq.each do |#{hm_singular_name}| %>
538
+ <tr><td><%= link_to(#{hm_singular_name}.brick_descrip, #{hm.first.klass.name.underscore}_path([#{obj_pk}])) %></td></tr>
533
539
  <% end %>
534
540
  <% end %>
535
541
  </table>"
@@ -5,7 +5,7 @@ module Brick
5
5
  module VERSION
6
6
  MAJOR = 1
7
7
  MINOR = 0
8
- TINY = 26
8
+ TINY = 27
9
9
 
10
10
  # PRE is nil unless it's a pre-release (beta, RC, etc.)
11
11
  PRE = nil
data/lib/brick.rb CHANGED
@@ -291,6 +291,10 @@ module Brick
291
291
  Brick.config.schema_to_analyse = schema
292
292
  end
293
293
 
294
+ def default_route_fallback=(resource_name)
295
+ Brick.config.default_route_fallback = resource_name
296
+ end
297
+
294
298
  # Load additional references (virtual foreign keys)
295
299
  # This is attempted early if a brick initialiser file is found, and then again as a failsafe at the end of our engine's initialisation
296
300
  # %%% Maybe look for differences the second time 'round and just add new stuff instead of entirely deferring
@@ -381,6 +385,9 @@ In config/initializers/brick.rb appropriate entries would look something like:
381
385
  def finalize!
382
386
  existing_controllers = routes.each_with_object({}) { |r, s| c = r.defaults[:controller]; s[c] = nil if c }
383
387
  ::Rails.application.routes.append do
388
+ unless ::Brick.config.default_route_fallback.blank? || ::Rails.application.routes.named_routes.send(:routes)[:root]
389
+ send(:root, "#{::Brick.config.default_route_fallback}#index")
390
+ end
384
391
  # %%% TODO: If no auto-controllers then enumerate the controllers folder in order to build matching routes
385
392
  # If auto-controllers and auto-models are both enabled then this makes sense:
386
393
  ::Brick.relations.each do |k, v|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brick
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.26
4
+ version: 1.0.27
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lorin Thwaits