lib/engine2/action.rb in engine2-1.0.8 vs lib/engine2/action.rb in engine2-1.0.9

- old
+ new

@@ -67,11 +67,11 @@ def invoke! handler if rmp = @request_action_proc action = self.class.new(node, assets, self) result = action.instance_exec(handler, *action.request_action_proc_params(handler), &rmp) action.post_process - response = @requestable ? (result || {}) : action.invoke(handler) + response = @requestable ? (result.is_a?(Hash) ? result : {}) : action.invoke(handler) response[:meta] = action.meta response else invoke(handler) end @@ -80,11 +80,11 @@ def repeat time @meta[:repeat] = time end def arguments args - @meta[:arguments] = args + (@meta[:arguments] ||= {}).merge! args end def execute command (@meta[:execute] ||= []) << command end @@ -233,11 +233,12 @@ end class RootAction < Action def initialize *args super - @meta.merge! environment: Handler::environment, application: Engine2::SETTINGS[:name], key_separator: Engine2::SETTINGS[:key_separator], ws_methods: ActionWebSocketSupport::WS_METHODS + @meta.merge! environment: Handler::environment, application: Engine2::SETTINGS[:name], uuid: SecureRandom.uuid, + key_separator: Engine2::SETTINGS[:key_separator], ws_methods: ActionWebSocketSupport::WS_METHODS end end module ActionAPISupport def fields field @@ -457,11 +458,11 @@ end end module ActionAngularSupport def ng_execute expr - (@meta[:execute] ||= "") << expr + ";" + (@meta[:execute] ||= String.new) << expr + ";" end def ng_record! name, value value = case value when String @@ -584,17 +585,16 @@ # params.merge({arguments: params.keys}) end end module ActionOnChangeSupport - def on_change field, &blk + def on_change field, trigger_on_start = false, &blk node_name = :"#{field}_on_change" - nd = node.define_node node_name, (blk.arity > 2 ? OnChangeGetAction : OnChangePostAction) + nd = node.define_node node_name, (blk.arity <= 2 ? OnChangeGetAction : OnChangePostAction) nd.*{request &blk} - fields! field, remote_onchange: node_name - fields! field, remote_onchange_record: :true if blk.arity > 2 + fields! field, remote_onchange: {action: node_name, record: blk.arity > 2, trigger_on_start: trigger_on_start} end class OnChangeAction < Action include ActionAPISupport, ActionAngularSupport @@ -606,13 +606,13 @@ params = handler.request.params [params["value"], params["record"]] end end - def invoke handler - {} - end + # def invoke handler + # {} + # end end class OnChangeGetAction < OnChangeAction action_type :on_change @@ -633,21 +633,21 @@ end end module ActionListSupport include ActionModelSupport, ActionAPISupport, ActionTabSupport, ActionPanelSupport, ActionMenuSupport, ActionOnChangeSupport, ActionDraggableSupport - attr_reader :filters, :orders + attr_reader :filters, :orders, :default_order_field def pre_run super config.merge!(per_page: 10, use_count: false, selectable: true) # search_active: false, panel_template 'scaffold/list' - panel_title "#{:list.icon} #{LOCS[assets[:model].name.to_sym]}" + panel_title "#{assets[:model].model_icon.icon} #{LOCS[assets[:model].model_route]}" loc! LOCS[:list_locs] menu :menu do - properties break: 2, group_class: "btn-group-xs" + properties break: 2, group_class: "btn-group-sm" option :search_toggle, icon: "search", show: "action.meta.search_field_list", active: "action.ui_state.search_active", button_loc: false # divider option :refresh, icon: "refresh", button_loc: false option :default_order, icon: "signal", button_loc: false divider @@ -655,11 +655,11 @@ option :show_meta, icon: "eye-open" end if Handler::development? end menu :item_menu do - properties break: 1, group_class: "btn-group-xs" + properties break: 1, group_class: "btn-group-sm" end @meta[:state] = [:query, :ui_state] end @@ -674,19 +674,20 @@ m.option_after :default_order, :select_toggle, icon: "check", enabled: "action.meta.config.selectable", active: "action.selection", button_loc: false end end def post_run + super + unless panel[:class] panel_class case @meta[:field_list].size when 1..3; '' when 4..6; 'modal-large' else; 'modal-huge' end end - super @meta[:primary_fields] = assets[:model].primary_keys end # def find_renderer type_info # renderer = DefaultSearchRenderers[type_info[:type]] || DefaultSearchRenderers[type_info[:otype]] @@ -742,12 +743,19 @@ def sortable *flds flds = @meta[:field_list] if flds.empty? fields! *flds, sort: true end + def default_order order + @default_order_field = order + end + def search_live *flds - flds = @meta[:search_field_list] if flds.empty? + if flds.empty? + flds = @meta[:search_field_list] + @meta[:disable_search_button] = true + end fields! *flds, search_live: true end def searchable *flds @meta.delete(:tab_list) @@ -768,12 +776,16 @@ 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 assets[:model].find_type_info(name)[:otype] == :string - filter(name){|handler, query, hash| query.where(name.ilike("%#{hash[name]}%")) } + model = assets[:model] + raise E2Error.new("Field '#{name}' needs to be a string") unless model.find_type_info(name)[:otype] == :string + filter name do |handler, query, hash| + value = hash[name] + value ? query.where(model.table_name.q(name).ilike("%#{value}%")) : query + end end def order name, &blk (@orders ||= {})[name] = blk end @@ -815,12 +827,15 @@ def allocate_record handler, json_rec model = assets[:model] handler.permit json_rec.is_a?(Hash) val_fields = (dynamic? ? static.validate_fields : @validate_fields) || model.type_info.keys - handler.permit (json_rec.keys - val_fields).empty? + left_fields = (json_rec.keys - val_fields) + puts "Left: #{left_fields.inspect}" unless left_fields.empty? + handler.permit left_fields.empty? + record = model.call(json_rec) record.validate_fields = val_fields record end @@ -1023,10 +1038,18 @@ end def hr_after field, message = '-' fields! field, hr: message end + + def create? + @action_type == :create + end + + def modify? + @action_type == :modify + end end module ActionCreateSupport include ActionFormSupport @@ -1035,11 +1058,11 @@ end def pre_run super panel_title "#{LOCS[:create_title]} - #{LOCS[assets[:model].table_name]}" - node.parent.*.menu(:menu).option_at 0, node.name, icon: "plus-sign", button_loc: false if node.parent.*.is_a?(ListAction) + node.parent.*.menu(:menu).option_at 0, node.name, icon: "plus-sign", button_loc: false if node.parent.*.is_a?(ActionListSupport) hide_pk unless assets[:model].natural_key end def record handler, record @@ -1303,10 +1326,14 @@ }, date: lambda{|action, field, info| action.fields(field)[:render] ||= {} action.fields(field)[:render].merge! format: info[:format], model_format: info[:model_format] # Model::DEFAULT_DATE_FORMAT }, + datetime: lambda{|action, field, info| + action.fields(field)[:render] ||= {} + action.fields(field)[:render].merge! format: info[:date_format], model_format: info[:date_model_format] # Model::DEFAULT_DATE_FORMAT + }, decimal_date: lambda{|action, field, info| SearchRendererPostProcessors[:date].(action, field, info) } ) @@ -1328,15 +1355,15 @@ decimal_date: lambda{|action, info| DefaultFormRenderers[:date].(action, info)}, decimal_time: lambda{|action, info| Templates.time_picker}, email: lambda{|action, info| Templates.email(info[:length])}, password: lambda{|action, info| Templates.password(info[:length])}, # date_range: lambda{|action, info| Templates.date_range}, - boolean: lambda{|action, info| Templates.checkbox_buttons(optional: !info[:required])}, + boolean: lambda{|action, info| Templates.checkbox_button}, currency: lambda{|action, info| Templates.currency}, list_select: lambda{|action, info| length = info[:values].length - max_length = info[:values].map(&:last).max_by(&:length).length + max_length = length > 0 ? info[:values].map(&:last).max_by(&:length).length : 0 if info[:multiselect] Templates.list_bsmselect(max_length) elsif length <= 3 Templates.list_buttons(optional: !info[:required]) elsif length <= 15 @@ -1358,9 +1385,10 @@ }, # required/opt ) (DefaultSearchRenderers ||= {}).merge!( date: lambda{|action, info| SearchTemplates.date_range}, + datetime: lambda{|action, info| SearchTemplates.date_range}, decimal_date: lambda{|action, info| SearchTemplates.date_range}, integer: lambda{|action, info| SearchTemplates.integer_range}, string: lambda{|action, info| SearchTemplates.input_text}, boolean: lambda{|action, info| SearchTemplates.checkbox_buttons}, list_select: lambda{|action, info|