brick 1.0.50 → 1.0.51

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: d5f5d009df0d4a7ce06a5dec8986d2664671e8b151f7493e5ce3f8f690ec7028
4
- data.tar.gz: 974e86906775728e0ff0c7022cfbd87578b7c3013a8f9a5fe0db2243dcad9bf1
3
+ metadata.gz: 9d0c2c69187f4d9b8eaa4a22777d3e517546bf355316012bf3e333064f8939b7
4
+ data.tar.gz: 33fcb2f6ca4373eba2db3a2891de392d9ef71ea693687328f72d23837cabdc46
5
5
  SHA512:
6
- metadata.gz: 0f652cf44f9d18eca35304d1660ed3967d39c60dff42bcc05043607fada17d427308b7052a4b474c71d8a65462f86970838771471b6f1dd7a89accabc31ddd27
7
- data.tar.gz: 386030a8bd3c603264ed9f534e1188eb5710fe67777ffc9634a9c318e8bac4153d2716bffbf9c34a7ff63999c3af39b2fa6db3b324abc5093467a33b144b41b8
6
+ metadata.gz: 7136cb61d19325c1916e9d2e8e6cd7ed6c1d6838c77a216c5275bf4cc0d3540eeabf5caae152b49b9f5cf0d5ea7307bebd98f2fdbc03e651310c1cfc6fbec436
7
+ data.tar.gz: 81007bc391e9871ab0bbe80fc5aaa440da2685e1abe16d65d09419e31e93529232757db173cf97ba78ad3e719af760cac2d83b57cb7d97fa488399d93d3d2f76
@@ -547,12 +547,23 @@ JOIN (SELECT #{selects.join(', ')}, COUNT(#{'DISTINCT ' if hm.options[:through]}
547
547
  this_module.const_set(module_name.to_sym, Module.new)
548
548
  end
549
549
  end
550
- if this_module.const_defined?(class_name = module_prefixes.last.to_sym)
551
- this_module.const_get(class_name)
552
- else
553
- # Build STI subclass and place it into the namespace module
554
- this_module.const_set(class_name, klass = Class.new(self))
555
- klass
550
+ begin
551
+ if this_module.const_defined?(class_name = module_prefixes.last.to_sym)
552
+ this_module.const_get(class_name)
553
+ else
554
+ # Build STI subclass and place it into the namespace module
555
+ this_module.const_set(class_name, klass = Class.new(self))
556
+ klass
557
+ end
558
+ rescue NameError => err
559
+ if column_names.include?(inheritance_column)
560
+ puts "Table #{table_name} has column #{inheritance_column} which ActiveRecord expects to use as its special inheritance column."
561
+ puts "Unfortunately the value \"#{type_name}\" does not seem to refer to a valid type name, greatly confusing matters. If that column is intended to be used for data and not STI, consider putting this line into your Brick initializer so that only for this table that column will not clash with ActiveRecord:"
562
+ puts " Brick.sti_type_column = { 'rails_#{inheritance_column}' => ['#{table_name}'] }"
563
+ self
564
+ else
565
+ raise
566
+ end
556
567
  end
557
568
  end
558
569
  end
@@ -603,6 +614,8 @@ Module.class_exec do
603
614
  return possible
604
615
  end
605
616
  class_name = args.first.to_s
617
+ # self.name is nil when a model name is requested in an .erb file
618
+ base_module = (self < ActiveRecord::Migration || !self.name) ? Object : self
606
619
  # See if a file is there in the same way that ActiveSupport::Dependencies#load_missing_constant
607
620
  # checks for it in ~/.rvm/gems/ruby-2.7.5/gems/activesupport-5.2.6.2/lib/active_support/dependencies.rb
608
621
  # that is, checking #qualified_name_for with: from_mod, const_name
@@ -610,14 +623,17 @@ Module.class_exec do
610
623
  # path_suffix = ActiveSupport::Dependencies.qualified_name_for(Object, args.first).underscore
611
624
  # return self._brick_const_missing(*args) if ActiveSupport::Dependencies.search_for_file(path_suffix)
612
625
  # If the file really exists, go and snag it:
613
- if !(is_found = ActiveSupport::Dependencies.search_for_file(class_name.underscore)) && (filepath = (self.name || class_name)&.split('::'))
614
- filepath = (filepath[0..-2] + [class_name]).join('/').underscore + '.rb'
615
- end
616
- if is_found
617
- return self._brick_const_missing(*args)
618
- # elsif ActiveSupport::Dependencies.search_for_file(filepath) # Last-ditch effort to pick this thing up before we fill in the gaps on our own
619
- # my_const = parent.const_missing(class_name) # ends up having: MyModule::MyClass
620
- # return my_const
626
+ if ActiveSupport::Dependencies.search_for_file(class_name.underscore)
627
+ return base_module._brick_const_missing(*args)
628
+ # elsif ActiveSupport::Dependencies.search_for_file(filepath) # Last-ditch effort to pick this thing up before we fill in the gaps on our own
629
+ # my_const = parent.const_missing(class_name) # ends up having: MyModule::MyClass
630
+ # return my_const
631
+ else
632
+ filepath = base_module.name&.split('::')&.[](0..-2) unless base_module == Object
633
+ filepath = ((filepath || []) + [class_name]).join('/').underscore + '.rb'
634
+ if ActiveSupport::Dependencies.search_for_file(filepath) # Last-ditch effort to pick this thing up before we fill in the gaps on our own
635
+ return base_module._brick_const_missing(*args)
636
+ end
621
637
  end
622
638
 
623
639
  relations = ::Brick.relations
@@ -636,7 +652,7 @@ Module.class_exec do
636
652
  Object.send(:build_controller, self, class_name, plural_class_name, model, relations)
637
653
  end
638
654
  elsif (::Brick.enable_models? || ::Brick.enable_controllers?) && # Schema match?
639
- self == Object && # %%% This works for Person::Person -- but also limits us to not being able to allow more than one level of namespacing
655
+ base_module == Object && # %%% This works for Person::Person -- but also limits us to not being able to allow more than one level of namespacing
640
656
  (schema_name = [(singular_table_name = class_name.underscore),
641
657
  (table_name = singular_table_name.pluralize),
642
658
  class_name,
@@ -644,7 +660,7 @@ Module.class_exec do
644
660
  (::Brick.config.sti_namespace_prefixes&.key?("::#{class_name}::") && class_name))
645
661
  # Build out a module for the schema if it's namespaced
646
662
  # schema_name = schema_name.camelize
647
- self.const_set(schema_name.to_sym, (built_module = Module.new))
663
+ base_module.const_set(schema_name.to_sym, (built_module = Module.new))
648
664
 
649
665
  [built_module, "module #{schema_name}; end\n"]
650
666
  # # %%% Perhaps an option to use the first module just as schema, and additional modules as namespace with a table name prefix applied
@@ -654,8 +670,8 @@ Module.class_exec do
654
670
  # See if a file is there in the same way that ActiveSupport::Dependencies#load_missing_constant
655
671
  # checks for it in ~/.rvm/gems/ruby-2.7.5/gems/activesupport-5.2.6.2/lib/active_support/dependencies.rb
656
672
 
657
- if (base_model = ::Brick.config.sti_namespace_prefixes&.fetch("::#{self.name}::", nil)&.constantize) || # Are we part of an auto-STI namespace? ...
658
- self != Object # ... or otherwise already in some namespace?
673
+ if (base_model = ::Brick.config.sti_namespace_prefixes&.fetch("::#{base_module.name}::", nil)&.constantize) || # Are we part of an auto-STI namespace? ...
674
+ base_module != Object # ... or otherwise already in some namespace?
659
675
  schema_name = [(singular_schema_name = name.underscore),
660
676
  (schema_name = singular_schema_name.pluralize),
661
677
  name,
@@ -668,7 +684,7 @@ Module.class_exec do
668
684
  if base_model
669
685
  schema_name = name.underscore # For the auto-STI namespace models
670
686
  table_name = base_model.table_name
671
- Object.send(:build_model, self, inheritable_name, model_name, singular_table_name, table_name, relations, table_name)
687
+ Object.send(:build_model, base_module, inheritable_name, model_name, singular_table_name, table_name, relations, table_name)
672
688
  else
673
689
  # Adjust for STI if we know of a base model for the requested model name
674
690
  # %%% Does not yet work with namespaced model names. Perhaps prefix with plural_class_name when doing the lookups here.
@@ -693,15 +709,15 @@ Module.class_exec do
693
709
  built_class
694
710
  elsif ::Brick.config.sti_namespace_prefixes&.key?("::#{class_name}") && !schema_name
695
711
  # module_prefixes = type_name.split('::')
696
- # path = self.name.split('::')[0..-2] + []
712
+ # path = base_module.name.split('::')[0..-2] + []
697
713
  # module_prefixes.unshift('') unless module_prefixes.first.blank?
698
714
  # candidate_file = Rails.root.join('app/models' + module_prefixes.map(&:underscore).join('/') + '.rb')
699
- self._brick_const_missing(*args)
700
- # elsif self != Object
715
+ base_module._brick_const_missing(*args)
716
+ # elsif base_module != Object
701
717
  # module_parent.const_missing(*args)
702
718
  else
703
- puts "MISSING! #{self.name} #{args.inspect} #{table_name}"
704
- self._brick_const_missing(*args)
719
+ puts "MISSING! #{base_module.name} #{args.inspect} #{table_name}"
720
+ base_module._brick_const_missing(*args)
705
721
  end
706
722
  end
707
723
  end
@@ -842,15 +858,15 @@ class Object
842
858
  "#{hmt_fk}_through_#{hm.first[:assoc_name]}"
843
859
  else # Use BT names to provide uniqueness
844
860
  if self.name.underscore.singularize == hm.first[:alternate_name]
845
- # Has previously been:
846
- # # If it folds back on itself then look at the other side
847
- # # (At this point just infer the source be the inverse of the first has_many that
848
- # # we find that is not ourselves. If there are more than two then uh oh, can't
849
- # # yet handle that rare circumstance!)
850
- # other = hms.find { |hm1| hm1 != hm } # .first[:fk]
851
- # options[:source] = other.first[:inverse][:assoc_name].to_sym
852
- # And also has been:
853
- # hm.first[:inverse][:assoc_name].to_sym
861
+ # Has previously been:
862
+ # # If it folds back on itself then look at the other side
863
+ # # (At this point just infer the source be the inverse of the first has_many that
864
+ # # we find that is not ourselves. If there are more than two then uh oh, can't
865
+ # # yet handle that rare circumstance!)
866
+ # other = hms.find { |hm1| hm1 != hm } # .first[:fk]
867
+ # options[:source] = other.first[:inverse][:assoc_name].to_sym
868
+ # And also has been:
869
+ # hm.first[:inverse][:assoc_name].to_sym
854
870
  options[:source] = hm.last.to_sym
855
871
  else
856
872
  through = hm.first[:alternate_name].pluralize
@@ -1135,7 +1151,17 @@ class Object
1135
1151
  # end
1136
1152
 
1137
1153
  is_need_params = true
1138
- # code << " # (Define :edit, and :destroy)\n"
1154
+ # code << " # (Define :destroy)\n"
1155
+ code << " def edit\n"
1156
+ code << find_by_id
1157
+ code << " end\n"
1158
+ self.define_method :edit do
1159
+ ::Brick.set_db_schema(params)
1160
+ id = is_pk_string ? params[:id] : params[:id]&.split(/[\/,_]/)
1161
+ id = id.first if id.is_a?(Array) && id.length == 1
1162
+ instance_variable_set("@#{singular_table_name}".to_sym, model.find(id))
1163
+ end
1164
+
1139
1165
  code << " def update\n"
1140
1166
  code << find_by_id
1141
1167
  params_name = "#{singular_table_name}_params"
@@ -1169,8 +1195,12 @@ class Object
1169
1195
  if is_need_params
1170
1196
  code << "private\n"
1171
1197
  code << " def #{params_name}\n"
1198
+ permits = model.columns_hash.keys.map { |c| c.to_sym.inspect } +
1199
+ model.reflect_on_all_associations.each_with_object([]) do |assoc, s|
1200
+ s << "#{assoc.name.to_s.singularize}_ids: []" if assoc.macro == :has_many && assoc.options[:through]
1201
+ end
1172
1202
  code << " params.require(:#{require_name = model.name.underscore.tr('/', '_')
1173
- }).permit(#{model.columns_hash.keys.map { |c| c.to_sym.inspect }.join(', ')})\n"
1203
+ }).permit(#{permits.join(', ')})\n"
1174
1204
  code << " end\n"
1175
1205
  self.define_method(params_name) do
1176
1206
  params.require(require_name.to_sym).permit(model.columns_hash.keys)
@@ -58,7 +58,9 @@ module Brick
58
58
  def template_exists?(*args, **options)
59
59
  (::Brick.config.add_orphans && args.first == 'orphans') ||
60
60
  _brick_template_exists?(*args, **options) ||
61
- set_brick_model(args)
61
+ # Do not auto-create a template when it's searching for an application.html.erb, which comes in like: ["edit", ["games", "application"]]
62
+ ((args[1].length == 1 || args[1][-1] != 'application') &&
63
+ set_brick_model(args))
62
64
  end
63
65
 
64
66
  def set_brick_model(find_args)
@@ -5,7 +5,7 @@ module Brick
5
5
  module VERSION
6
6
  MAJOR = 1
7
7
  MINOR = 0
8
- TINY = 50
8
+ TINY = 51
9
9
 
10
10
  # PRE is nil unless it's a pre-release (beta, RC, etc.)
11
11
  PRE = nil
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brick
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.50
4
+ version: 1.0.51
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lorin Thwaits
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-28 00:00:00.000000000 Z
11
+ date: 2022-08-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord