brick 1.0.5 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/brick/extensions.rb +20 -2
- data/lib/brick/frameworks/rails/engine.rb +11 -5
- data/lib/brick/version_number.rb +1 -1
- data/lib/brick.rb +5 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 634669de2074746d62642b8c9108070b78e92b14f7924ac969404b240de2267d
|
4
|
+
data.tar.gz: 7614fa456fcaeb6bc68227230088fe3adeed31278241a000f3fb19792b763d26
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3b89ed9c8ffe3c63e098a3f7a8e5a9987db0c00bfe2c917b7c2acefebc30653a4a88d091852b8f8e6128daa310e7c7742d10611acfb0ffe7d06cc6c01da7068e
|
7
|
+
data.tar.gz: 98d58b4461b9e85537da640e3940316553d142a341547086ce614f75d9366145c8818beb6e972ac93a77e7209b034eb64a64eb9a078f6afb91fbbd008e3ade69
|
data/lib/brick/extensions.rb
CHANGED
@@ -80,6 +80,18 @@ module ActiveRecord
|
|
80
80
|
end
|
81
81
|
end
|
82
82
|
end
|
83
|
+
|
84
|
+
module Inheritance
|
85
|
+
module ClassMethods
|
86
|
+
private
|
87
|
+
|
88
|
+
alias _brick_find_sti_class find_sti_class
|
89
|
+
def find_sti_class(type_name)
|
90
|
+
::Brick.sti_models[type_name] = { base: self } unless type_name.blank?
|
91
|
+
_brick_find_sti_class(type_name)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
83
95
|
end
|
84
96
|
|
85
97
|
# Object.class_exec do
|
@@ -113,6 +125,11 @@ class Object
|
|
113
125
|
singular_table_name = ActiveSupport::Inflector.underscore(model_name)
|
114
126
|
table_name = ActiveSupport::Inflector.pluralize(singular_table_name)
|
115
127
|
|
128
|
+
# Adjust for STI if we know of a base model for the requested model name
|
129
|
+
if (base_model = ::Brick.sti_models[model_name]&.fetch(:base, nil))
|
130
|
+
table_name = base_model.table_name
|
131
|
+
end
|
132
|
+
|
116
133
|
# Maybe, just maybe there's a database table that will satisfy this need
|
117
134
|
if (matching = [table_name, singular_table_name, plural_class_name, model_name].find { |m| relations.key?(m) })
|
118
135
|
build_model(model_name, singular_table_name, table_name, relations, matching)
|
@@ -138,8 +155,9 @@ class Object
|
|
138
155
|
if table_name == singular_table_name && !ActiveSupport::Inflector.inflections.uncountable.include?(table_name)
|
139
156
|
raise NameError.new("Class name for a model that references table \"#{matching}\" should be \"#{ActiveSupport::Inflector.singularize(model_name)}\".")
|
140
157
|
end
|
141
|
-
|
142
|
-
|
158
|
+
base_model = ::Brick.sti_models[model_name]&.fetch(:base, nil) || ActiveRecord::Base
|
159
|
+
code = +"class #{model_name} < #{base_model.name}\n"
|
160
|
+
built_model = Class.new(base_model) do |new_model_class|
|
143
161
|
Object.const_set(model_name.to_sym, new_model_class)
|
144
162
|
# Accommodate singular or camel-cased table names such as "order_detail" or "OrderDetails"
|
145
163
|
code << " self.table_name = '#{self.table_name = matching}'\n" unless table_name == matching
|
@@ -66,7 +66,7 @@ module Brick
|
|
66
66
|
associatives = hms.select { |k, v| v.options[:through] }.each_with_object({}) do |hmt, s|
|
67
67
|
s[hmt.first] = hms.delete(hmt.last.options[:through]) # End up with a hash of HMT names pointing to join-table associations
|
68
68
|
end
|
69
|
-
hms_headers = hms.each_with_object(+'') { |hm, s| s << "<th>
|
69
|
+
hms_headers = hms.each_with_object(+'') { |hm, s| s << "<th>H#{hm.last.macro == :has_one ? 'O' : 'M'}#{'T' if hm.last.options[:through]} #{hm.first}</th>\n" }
|
70
70
|
hms_columns = hms.each_with_object(+'') do |hm, s|
|
71
71
|
hm_fk_name = if hm.last.options[:through]
|
72
72
|
associative = associatives[hm.last.name]
|
@@ -74,9 +74,15 @@ module Brick
|
|
74
74
|
else
|
75
75
|
hm.last.foreign_key
|
76
76
|
end
|
77
|
-
s <<
|
77
|
+
s << if hm.last.macro == :has_many
|
78
|
+
"<td>
|
78
79
|
<%= link_to \"#\{#{obj_name}.#{hm.first}.count\} #{hm.first}\", #{hm.last.klass.name.underscore.pluralize}_path({ #{hm_fk_name}: #{obj_name}.#{pk} }) unless #{obj_name}.#{hm.first}.count.zero? %>
|
79
80
|
</td>\n"
|
81
|
+
else # has_one
|
82
|
+
"<td>
|
83
|
+
<%= obj = #{obj_name}.#{hm.first}; link_to(obj.brick_descrip, obj) if obj %>
|
84
|
+
</td>\n"
|
85
|
+
end
|
80
86
|
end
|
81
87
|
|
82
88
|
inline = case args.first
|
@@ -89,7 +95,7 @@ module Brick
|
|
89
95
|
<table id=\"#{table_name}\">
|
90
96
|
<tr>
|
91
97
|
<% is_first = true; is_need_id_col = nil
|
92
|
-
bts = { #{bts.each_with_object([]) { |v, s| s << "#{v.first.inspect} => [#{v.last.first.inspect}, #{v.last.
|
98
|
+
bts = { #{bts.each_with_object([]) { |v, s| s << "#{v.first.inspect} => [#{v.last.first.inspect}, #{v.last[1].name}, #{v.last[1].primary_key.inspect}]"}.join(', ')} }
|
93
99
|
@#{table_name}.columns.map(&:name).each do |col| %>
|
94
100
|
<% next if col == '#{pk}' || ::Brick.config.metadata_columns.include?(col) %>
|
95
101
|
<th>
|
@@ -125,9 +131,9 @@ module Brick
|
|
125
131
|
<% next if k == '#{pk}' || ::Brick.config.metadata_columns.include?(k) %>
|
126
132
|
<td>
|
127
133
|
<% if (bt = bts[k]) %>
|
128
|
-
<%= obj = bt[1].find_by(bt.last => val); link_to
|
134
|
+
<%= obj = bt[1].find_by(bt.last => val); link_to(obj.brick_descrip, obj) if obj %>
|
129
135
|
<% elsif is_first %>
|
130
|
-
<%= is_first = false; link_to val, #{obj_name} %>
|
136
|
+
<%= is_first = false; link_to val, #{obj_name}_path(#{obj_name}.#{pk}) %>
|
131
137
|
<% else %>
|
132
138
|
<%= val %>
|
133
139
|
<% end %>
|
data/lib/brick/version_number.rb
CHANGED
data/lib/brick.rb
CHANGED
@@ -81,6 +81,10 @@ if Gem::Specification.all_names.any? { |g| g.start_with?('rails-') }
|
|
81
81
|
require 'brick/frameworks/rails'
|
82
82
|
end
|
83
83
|
module Brick
|
84
|
+
def self.sti_models
|
85
|
+
@sti_models ||= {}
|
86
|
+
end
|
87
|
+
|
84
88
|
class << self
|
85
89
|
# All tables and views (what Postgres calls "relations" including column and foreign key info)
|
86
90
|
def relations
|
@@ -107,7 +111,7 @@ module Brick
|
|
107
111
|
end
|
108
112
|
|
109
113
|
s.first[a.foreign_key] = [a.name, a.klass]
|
110
|
-
when :has_many
|
114
|
+
when :has_many, :has_one
|
111
115
|
s.last[a.name] = a
|
112
116
|
end
|
113
117
|
s
|