lib/engine2/core.rb in engine2-1.0.5 vs lib/engine2/core.rb in engine2-1.0.6

- old
+ new

@@ -326,10 +326,37 @@ Hash[primary_keys_qualified.zip(id)] end end module DatasetMethods + def load *args + if entry = self[*args] + model.after_load_processors.each do |name, proc| + type_info = model.find_type_info(name) + name_sym = name.to_sym + proc.(entry, name_sym, type_info) if entry.key?(name_sym) + end if model.after_load_processors + entry + end + end + + def load_all + entries = self.all + apply_after_load_processors(model, entries) if model.after_load_processors + entries + end + + def apply_after_load_processors model, entries + model.after_load_processors.each do |name, proc| + type_info = model.find_type_info(name) + name_sym = name.to_sym + entries.each do |entry| + proc.(entry, name_sym, type_info) if entry.key?(name_sym) + end + end + end + def ensure_primary_key pk = model.primary_keys raise Engine2::E2Error.new("No primary key defined for model #{model}") unless pk && pk.all? if opts_select = @opts[:select] @@ -374,16 +401,16 @@ else raise Engine2::E2Error.new("Unknown selection #{sel}") end end - def setup! fields + def setup_query fields joins = {} type_info = model.type_info model_table_name = model.table_name - @opts[:select] = @opts[:select].map do |sel| + select = @opts[:select].map do |sel| extract_select sel do |table, name, aliaz| info = if table if table == model_table_name model else @@ -414,15 +441,13 @@ else qname end end end - end + end.compact - @opts[:select].compact!.freeze - - joins.reduce(self) do |joined, (table, assoc)| + joins.reduce(clone(select: select)) do |joined, (table, assoc)| m = assoc.associated_class case assoc[:type] when :many_to_one keys = assoc[:qualified_key] joined.left_join(table, m.primary_keys.zip(keys.is_a?(Array) ? keys : [keys])) @@ -519,10 +544,13 @@ @model_boot_blk.() if @model_boot_blk load 'engine2/models/Files.rb' load 'engine2/models/UserInfo.rb' Dir["#{Engine2::SETTINGS.path_for(:model_path)}/*"].each{|m| load m} puts "MODELS: #{Sequel::DATABASES.reduce(0){|s, d|s + d.models.size}}, Time: #{Time.now - t}" - Sequel::DATABASES.each &:dump_schema_cache_to_file + Sequel::DATABASES.each do |db| + db.dump_schema_cache_to_file + db.models.each{|n, m|m.synchronize_type_info} + end send(:remove_const, :ROOT) if defined? ROOT const_set(:ROOT, ActionNode.new(nil, :api, RootAction, {})) @boot_blk.(ROOT)