Module: ModalLogic::Helpers
- Defined in:
- lib/modal_logic/helpers.rb
Instance Method Summary collapse
- #modal_form_response(model, opts = {}) ⇒ Object
- #modal_title(model) ⇒ Object
- #path_to_current_controller_form(opts = {}) ⇒ Object
Instance Method Details
#modal_form_response(model, opts = {}) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/modal_logic/helpers.rb', line 3 def modal_form_response( model, opts = {}) response = {} case params[:action] when 'new', 'edit' response[:body] = render_to_string path_to_current_controller_form, layout: false response[:title] = opts[:title] || modal_title(model) when 'create', 'update' if ! model.valid? || ! model.persisted? || opts[:errors].present? response[:body] = render_to_string path_to_current_controller_form, layout: false response[:errors] = opts[:errors] || model.errors response[:flash] = flash response[:title] = opts[:title] || modal_title(model) else response[:redirect_location] = opts[:redirect_location] if opts[:redirect_location] response[:close] = true end end response end |
#modal_title(model) ⇒ Object
25 26 27 28 |
# File 'lib/modal_logic/helpers.rb', line 25 def modal_title(model) action = model.persisted? ? :edit : :new "#{action.to_s.titleize} #{File.basename(params[:controller]).singularize.titleize}" #without namespace (i.e. /admin/collections) end |
#path_to_current_controller_form(opts = {}) ⇒ Object
30 31 32 |
# File 'lib/modal_logic/helpers.rb', line 30 def path_to_current_controller_form( opts = {} ) File.join(Rails.root, 'app/views', params[:controller], opts[:filename] || '_form') end |