Module: EffectiveResourcesHelper
- Defined in:
- app/helpers/effective_resources_helper.rb
Instance Method Summary collapse
-
#effective_submit(form, options = {}, &block) ⇒ Object
effective_bootstrap.
- #format_resource_value(value) ⇒ Object
-
#render_resource_actions(resource, atts = {}, &block) ⇒ Object
partial: :dropleft|:glyphicons|string locals: {} render locals you can also pass all action names and true/false such as edit: true, show: false.
- #render_resource_buttons(resource, atts = {}, &block) ⇒ Object
-
#render_resource_form(resource, atts = {}) ⇒ Object
When called from /admin/things/new.html.haml this will render ‘admin/things/form’, or ‘things/form’, or ‘thing/form’.
-
#render_resource_partial(resource, atts = {}) ⇒ Object
Similar to render_resource_form.
-
#simple_form_submit(form, options = {}, &block) ⇒ Object
effective_form_inputs.
-
#tableize_hash(obj, table: 'table', th: true, sub_table: 'table', sub_th: true, flatten: true) ⇒ Object
Tableize attributes This is used by effective_orders, effective_logging, effective_trash and effective_mergery.
Instance Method Details
#effective_submit(form, options = {}, &block) ⇒ Object
effective_bootstrap
4 5 6 7 8 9 10 11 12 13 14 |
# File 'app/helpers/effective_resources_helper.rb', line 4 def effective_submit(form, = {}, &block) actions = (controller.respond_to?(:effective_resource) ? controller.class : find_effective_resource).submits actions = actions.select { |k, v| v[:default] != true } if .delete(:defaults) == false actions = permitted_resource_actions(form.object, actions) submits = actions.map { |name, opts| form.save(name, opts.except(:action, :title, 'data-method') ) }.join.html_safe form.submit('', ) do (block_given? ? capture(&block) : ''.html_safe) + submits end end |
#format_resource_value(value) ⇒ Object
203 204 205 206 207 208 |
# File 'app/helpers/effective_resources_helper.rb', line 203 def format_resource_value(value) @format_resource_tags ||= ActionView::Base..to_a + ['table', 'thead', 'tbody', 'tfoot', 'tr', 'td', 'th'] @format_resource_atts ||= ActionView::Base.sanitized_allowed_attributes.to_a + ['colspan', 'rowspan'] simple_format(sanitize(value.to_s, tags: @format_resource_tags, attributes: @format_resource_atts), {}, sanitize: false) end |
#render_resource_actions(resource, atts = {}, &block) ⇒ Object
partial: :dropleft|:glyphicons|string locals: {} render locals you can also pass all action names and true/false such as edit: true, show: false
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'app/helpers/effective_resources_helper.rb', line 59 def render_resource_actions(resource, atts = {}, &block) unless resource.kind_of?(ActiveRecord::Base) || resource.kind_of?(Class) || resource.kind_of?(Array) || resource.class.ancestors.include?(ActiveModel::Model) raise 'expected first argument to be an ActiveRecord::Base object or Array of objects' end raise 'expected attributes to be a Hash' unless atts.kind_of?(Hash) btn_class = atts[:btn_class] effective_resource = atts[:effective_resource] namespace = atts[:controller_namespace] || atts[:namespace] locals = atts[:locals] || {} partial = atts[:partial] spacer_template = locals[:spacer_template] effective_resource ||= find_effective_resource(resource) namespace ||= (effective_resource.namespace.to_sym if effective_resource.namespace) # Assign actions actions = if atts.key?(:actions) # We filter out any actions passed to us that aren't supported available = effective_resource.actions + atts[:actions].map { |k, v| v[:action] if v[:path] }.compact atts[:actions].inject({}) { |h, (commit, opts)| h[commit] = opts if available.include?(opts[:action]); h } else (resource.kind_of?(Class) ? effective_resource.resource_klass_actions : effective_resource.resource_actions) end # Consider only, except, false and proc false only = Array(atts[:only]).compact except = Array(atts[:except]).compact actions = actions.select do |_, opts| action = opts[:action] if only.present? && !only.include?(action) false elsif except.present? && except.include?(action) false elsif atts[action].respond_to?(:call) Effective::ResourceExec.new(self, resource).instance_exec(&atts[action]) else atts[action] != false end end # Select Partial partial = ['effective/resource/actions', partial.to_s].join('_') if partial.kind_of?(Symbol) partial = (partial.presence || 'effective/resource/actions') + '.html' # Assign Locals locals = { resource: resource, effective_resource: effective_resource, format_block: (block if block_given?), namespace: namespace, actions: actions, btn_class: (btn_class || '') }.compact.merge(locals) if resource.kind_of?(Array) render(partial: partial, collection: resource, as: :resource, locals: locals.except(:resource), spacer_template: spacer_template) else render(partial, locals) end end |
#render_resource_buttons(resource, atts = {}, &block) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'app/helpers/effective_resources_helper.rb', line 37 def (resource, atts = {}, &block) effective_resource = find_effective_resource actions = (controller.respond_to?(:effective_resource) ? controller.class : effective_resource). actions = if resource.kind_of?(Class) actions.select { |_, v| effective_resource.collection_get_actions.include?(v[:action]) } elsif resource.respond_to?(:persisted?) && resource.persisted? actions.select { |_, v| effective_resource.member_actions.include?(v[:action]) } else {} end render_resource_actions(resource, atts.merge(actions: actions), &block) end |
#render_resource_form(resource, atts = {}) ⇒ Object
When called from /admin/things/new.html.haml this will render ‘admin/things/form’, or ‘things/form’, or ‘thing/form’
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'app/helpers/effective_resources_helper.rb', line 124 def render_resource_form(resource, atts = {}) unless resource.kind_of?(ActiveRecord::Base) || resource.class.ancestors.include?(ActiveModel::Model) raise 'expected first argument to be an ActiveRecord or ActiveModel object' end raise 'expected attributes to be a Hash' unless atts.kind_of?(Hash) effective_resource = (atts.delete(:effective_resource) || find_effective_resource) action = atts.delete(:action) atts = { :namespace => (effective_resource.namespace.to_sym if effective_resource.namespace), effective_resource.name.to_sym => resource }.compact.merge(atts) if lookup_context.template_exists?("form_#{action}", controller._prefixes, :partial) render "form_#{action}", atts elsif lookup_context.template_exists?('form', controller._prefixes, :partial) render 'form', atts elsif lookup_context.template_exists?('form', effective_resource.plural_name, :partial) render "#{effective_resource.plural_name}/form", atts elsif lookup_context.template_exists?('form', effective_resource.name, :partial) render "#{effective_resource.name}/form", atts else render 'form', atts # Will raise the regular error end end |
#render_resource_partial(resource, atts = {}) ⇒ Object
Similar to render_resource_form
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'app/helpers/effective_resources_helper.rb', line 150 def render_resource_partial(resource, atts = {}) unless resource.kind_of?(ActiveRecord::Base) || resource.class.ancestors.include?(ActiveModel::Model) raise 'expected first argument to be an ActiveRecord or ActiveModel object' end raise 'expected attributes to be a Hash' unless atts.kind_of?(Hash) effective_resource = (atts.delete(:effective_resource) || find_effective_resource) action = atts.delete(:action) atts = { :namespace => (effective_resource.namespace.to_sym if effective_resource.namespace), effective_resource.name.to_sym => resource }.compact.merge(atts) if lookup_context.template_exists?(effective_resource.name, controller._prefixes, :partial) render(effective_resource.name, atts) elsif lookup_context.template_exists?(effective_resource.name, [effective_resource.plural_name], :partial) render(effective_resource.plural_name + '/' + effective_resource.name, atts) elsif lookup_context.template_exists?(effective_resource.name, [effective_resource.name], :partial) render(effective_resource.name + '/' + effective_resource.name, atts) else render(resource, atts) # Will raise the regular error end end |
#simple_form_submit(form, options = {}, &block) ⇒ Object
effective_form_inputs
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'app/helpers/effective_resources_helper.rb', line 17 def simple_form_submit(form, = {}, &block) actions = (controller.respond_to?(:effective_resource) ? controller.class : find_effective_resource).submits actions = permitted_resource_actions(form.object, actions) submits = actions.map { |name, opts| form.(:submit, name, opts.except(:action, :title, 'data-method')) } # I think this is a bug. I can't override default button class when passing my own class: variable. it merges them. if (btn_class = SimpleForm.).present? submits = submits.map { |submit| submit.sub(btn_class, '') } end submits = submits.join(' ').html_safe = { class: 'form-actions' }.merge(.delete(:wrapper_html) || {}) content_tag(:div, ) do (block_given? ? capture(&block) : ''.html_safe) + submits end end |
#tableize_hash(obj, table: 'table', th: true, sub_table: 'table', sub_th: true, flatten: true) ⇒ Object
Tableize attributes This is used by effective_orders, effective_logging, effective_trash and effective_mergery
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'app/helpers/effective_resources_helper.rb', line 175 def tableize_hash(obj, table: 'table', th: true, sub_table: 'table', sub_th: true, flatten: true) case obj when Hash if flatten && obj[:attributes].kind_of?(Hash) obj = obj[:attributes].merge(obj.except(:attributes)) end content_tag(:table, class: table.presence) do content_tag(:tbody) do obj.map do |key, value| content_tag(:tr, class: key.to_param) do content_tag((th == true ? :th : :td), key) + content_tag(:td) { tableize_hash(value, table: sub_table, th: sub_th, sub_table: sub_table, sub_th: sub_th, flatten: flatten) } end end.join.html_safe end end when Array obj.map { |value| tableize_hash(value, table: sub_table, th: sub_th, sub_table: sub_table, sub_th: sub_th, flatten: flatten) }.join('<br>') when Symbol ":#{obj}" when NilClass '-' else obj.to_s.presence || '""' end.html_safe end |