lib/engine2/meta.rb in engine2-1.0.3 vs lib/engine2/meta.rb in engine2-1.0.4
- old
+ new
@@ -1,9 +1,9 @@
# coding: utf-8
module Engine2
class Meta
- attr_reader :action, :assets, :invokable, :static
+ attr_reader :action, :assets, :static, :invokable
class << self
def meta_type mt = nil
mt ? @meta_type = mt : @meta_type
end
@@ -36,23 +36,40 @@
def check_static_meta
raise E2Error.new("Static meta required") if dynamic?
end
+ def define_invoke &blk
+ check_static_meta
+ self.class.class_eval{define_method :invoke, &blk}
+ end
+
def invoke! handler
if rmp = @request_meta_proc
meta = self.class.new(action, assets, self)
- meta.instance_exec(handler, *meta.request_meta_proc_params(handler), &rmp)
+ meta_result = meta.instance_exec(handler, *meta.request_meta_proc_params(handler), &rmp)
meta.post_process
- response = meta.invoke(handler)
+ response = @requestable ? (meta_result || {}) : meta.invoke(handler)
response[:meta] = meta.get
response
else
invoke(handler)
end
end
+ def repeat time
+ @meta[:repeat] = time
+ end
+
+ def arguments args
+ @meta[:arguments] = args
+ end
+
+ def execute time
+ @meta[:execute] = time
+ end
+
def get
@meta
end
def dynamic?
@@ -112,11 +129,20 @@
def action_defined
end
def post_run
- @invokable = respond_to?(:invoke)
+ if respond_to? :invoke
+ @invokable = true
+ else
+ if @request_meta_proc
+ @invokable = true
+ @requestable = true
+ else
+ @meta[:invokable] = false
+ end
+ end
post_process
end
def post_process
end
@@ -124,19 +150,18 @@
def split_keys id
Sequel::split_keys(id)
end
end
- class DummyMeta < Meta
- meta_type :dummy
+ class InlineMeta < Meta
+ meta_type :inline
end
class RootMeta < Meta
def initialize *args
super
- @meta[:environment] = Handler::environment
- @meta[:application] = Engine2::app_name
+ @meta.merge! environment: Handler::environment, application: Engine2::SETTINGS[:name], key_separator: Engine2::SETTINGS[:key_separator]
end
end
module MetaAPISupport
def reload_routes!
@@ -218,11 +243,11 @@
raise E2Error.new("Model not found in tree for action: #{action.name}") unless act
mdl = act.*.assets[:model]
end until mdl
if asc = @assets[:assoc]
- @assets[:model] = Object.const_get(asc[:class_name])
+ @assets[:model] = asc.associated_class
# raise E2Error.new("Association '#{asc}' for model '#{asc[:class_name]}' not found") unless @assets[:model]
else
@assets[:model] = mdl
asc = act.*.assets[:assoc]
@assets[:assoc] = asc if asc
@@ -248,11 +273,11 @@
info = model.type_info[name]
unless info
if name =~ /^(\w+)__(\w+?)$/ # (?:___\w+)?
assoc = model.many_to_one_associations[$1.to_sym] || model.one_to_one_associations[$1.to_sym]
raise E2Error.new("Association #{$1} not found for model #{model}") unless assoc
- m = Object.const_get(assoc[:class_name])
+ m = assoc.associated_class
info = m.type_info.fetch($2.to_sym)
else
raise E2Error.new("Type info not found for '#{name}' in model '#{model}'")
end
end
@@ -544,11 +569,11 @@
include MetaModelSupport, MetaAPISupport, MetaTabSupport, MetaPanelSupport, MetaMenuSupport, MetaOnChangeSupport
attr_reader :filters, :orders
def pre_run
super
- config.merge!(per_page: 10, use_count: false, show_item_menu: true, selectable: true) # search_active: false,
+ config.merge!(per_page: 10, use_count: false, selectable: true) # search_active: false,
panel_template 'scaffold/list'
panel_panel_template 'panels/menu_m' unless action.parent.*.assets[:model]
search_template 'scaffold/search'
panel_title "#{:list.icon} #{LOCS[assets[:model].name.to_sym]}"
@@ -570,10 +595,15 @@
end
@meta[:state] = [:query, :ui_state]
end
+ def field_tabs hash
+ super
+ search_template 'scaffold/search_tabs'
+ end
+
def select_toggle_menu
m = menu :menu
unless m.option_index(:select_toggle, false)
m.option_after :default_order, :select_toggle, icon: "check", enabled: "action.meta.config.selectable", active: "action.selection", button_loc: false
end
@@ -668,10 +698,15 @@
def filter name, &blk
(@filters ||= {})[name] = blk
end
+ def filter_case_insensitive name
+ raise E2Error.new("Field '#{name}' needs to be a string one") unless get_type_info(name)[:otype] == :string
+ filter(name){|query, hash, handler| query.where(name.ilike("%#{hash[name]}%")) }
+ end
+
def order name, &blk
(@orders ||= {})[name] = blk
end
end
@@ -768,10 +803,15 @@
option :cancel, icon: "remove" # text: true,
end
# modal_action false
end
+ def field_tabs hash
+ super
+ panel_template 'scaffold/form_tabs'
+ end
+
def record handler, record
end
def post_process
if fields = @meta[:fields]
@@ -907,15 +947,21 @@
def pre_run
super
panel_template 'scaffold/view'
panel_title LOCS[:view_title]
+ panel[:backdrop] = true
menu(:panel_menu).option :cancel, icon: "remove"
action.parent.*.menu(:item_menu).option action.name, icon: "file", button_loc: false
end
+ def field_tabs hash
+ super
+ panel_template 'scaffold/view_tabs'
+ end
+
def record handler, record
end
def invoke handler
handler.permit id = handler.params[:id]
@@ -1000,10 +1046,13 @@
meta.info! field, type: :decimal_time
},
datetime: lambda{|meta, field, info|
meta.info[field][:render].merge! date_format: info[:date_format], time_format: info[:time_format], date_model_format: info[:date_model_format], time_model_format: info[:time_model_format]
},
+ currency: lambda{|meta, field, info|
+ meta.info[field][:render].merge! symbol: info[:symbol]
+ },
# date_range: lambda{|meta, field, info|
# meta.info[field][:render].merge! other_date: info[:other_date], format: info[:format], model_format: info[:model_format]
# meta.hide_fields info[:other_date]
# meta.info[field][:decimal_date] = true if info[:validations][:decimal_date]
# },
@@ -1022,18 +1071,14 @@
f_info[:type] = meta.assets[:model].type_info[of].fetch(:otype)
end
},
file_store: lambda{|meta, field, info|
meta.info[field][:render].merge! multiple: info[:multiple]
- # meta[:model] = meta.action.model.table_name
},
star_to_many_field: lambda{|meta, field, info|
field_info = meta.info[field]
field_info[:assoc] = :"#{info[:assoc_name]}!"
- # meta.info[field][:render].merge! multiple: info[:multiple]
- # field_info = meta.info[field]
- # field_info[:resource] ||= "#{Handler::API}#{meta.model.namespace}/#{info[:assoc_name]}"
}
)
(ListRendererPostProcessors ||= {}).merge!(
boolean: lambda{|meta, field, info|
@@ -1065,18 +1110,10 @@
model = meta.assets[:model]
if model.type_info[field]
keys = info[:keys]
else
meta.check_static_meta
- model = Object.const_get(model.many_to_one_associations[field[/^\w+?(?=__)/].to_sym][:class_name])
- # meta.action.define_action :"#{info[:assoc_name]}!" do # assoc_#{aname}
- # define_action :decode, DecodeEntryMeta, assoc: model.association_reflections[info[:assoc_name]] do
- # run_scheme :default_many_to_one
- # end
- # end
-
- # verify associations ?
- # model = Model.models.fetch(field[/^\w+?(?=__)/].to_sym)
+ model = model.many_to_one_associations[field[/^\w+?(?=__)/].to_sym].associated_class
keys = info[:keys].map{|k| :"#{model.table_name}__#{k}"}
end
field_info = meta.info[field]
field_info[:assoc] = :"#{info[:assoc_name]}!"