Class: MerbAdmin::Forms
- Inherits:
-
Application
- Object
- Merb::Controller
- Application
- MerbAdmin::Forms
- Defined in:
- app/controllers/forms.rb
Instance Method Summary collapse
- #create ⇒ Object
- #delete(id) ⇒ Object
- #destroy(id) ⇒ Object
- #edit(id) ⇒ Object
- #index ⇒ Object
- #list ⇒ Object
- #new ⇒ Object
- #update(id) ⇒ Object
Methods inherited from Application
Instance Method Details
#create ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'app/controllers/forms.rb', line 64 def create instance = eval("params[:#{@model_name.snake_case}]") @instance = @model.new(instance) if @instance.save if params[:_continue] redirect slice_url(:admin_edit, :model_name => @model_name.snake_case, :id => @instance.id), :message => {:notice => "#{@model_name} was successfully created"} elsif params[:_add_another] redirect slice_url(:admin_new, :model_name => @model_name.snake_case), :message => {:notice => "#{@model_name} was successfully created"} else redirect slice_url(:admin_list, :model_name => @model_name.snake_case), :message => {:notice => "#{@model_name} was successfully created"} end else [:error] = "#{@model_name} failed to be created" render(:new, :layout => "form") end end |
#delete(id) ⇒ Object
99 100 101 102 103 |
# File 'app/controllers/forms.rb', line 99 def delete(id) @instance = @model.get(id) raise NotFound unless @instance render(:layout => "form") end |
#destroy(id) ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'app/controllers/forms.rb', line 105 def destroy(id) @instance = @model.get(id) raise NotFound unless @instance if @instance.destroy redirect slice_url(:admin_list, :model_name => @model_name.snake_case), :message => {:notice => "#{@model_name} was successfully destroyed"} else raise InternalServerError end end |
#edit(id) ⇒ Object
58 59 60 61 62 |
# File 'app/controllers/forms.rb', line 58 def edit(id) @instance = @model.get(id) raise NotFound unless @instance render(:layout => "form") end |
#index ⇒ Object
4 5 6 7 8 9 |
# File 'app/controllers/forms.rb', line 4 def index @models = DataMapper::Resource.descendants.to_a.sort{|a, b| a.to_s <=> b.to_s} # remove DataMapperSessionStore because it's included by default @models -= [Merb::DataMapperSessionStore] if Merb.const_defined?(:DataMapperSessionStore) render(:layout => "dashboard") end |
#list ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'app/controllers/forms.rb', line 11 def list = {} filters = params[:filter] || {} filters.each_pair do |key, value| if @model.properties[key].primitive.to_s == "TrueClass" .merge!(key.to_sym => (value == "true" ? true : false)) elsif @model.properties[key].primitive.to_s == "Integer" && @model.properties[key].type.respond_to?(:flag_map) .merge!(key.to_sym => value.to_sym) end end if params[:all] = { :limit => 200, }.merge() @instances = @model.all().reverse else if params[:query] condition_statement = [] conditions = [] @properties.each do |property| next unless property.primitive.to_s == "String" condition_statement << "#{property.field} LIKE ?" conditions << "%#{params[:query]}%" end conditions.unshift(condition_statement.join(" OR ")) .merge!(:conditions => conditions) unless conditions == [""] end # monkey patch pagination @model.class_eval("is_paginated") unless @model.respond_to?(:paginated) @current_page = (params[:page] || 1).to_i = { :page => @current_page, :per_page => 100, }.merge() @page_count, @instances = @model.paginated() .delete(:page) .delete(:per_page) end @record_count = @model.count() render(:layout => "list") end |
#new ⇒ Object
53 54 55 56 |
# File 'app/controllers/forms.rb', line 53 def new @instance = @model.new render(:layout => "form") end |
#update(id) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'app/controllers/forms.rb', line 81 def update(id) instance = eval("params[:#{@model_name.snake_case}]") @instance = @model.get(id) raise NotFound unless @instance if @instance.update_attributes(instance) if params[:_continue] redirect slice_url(:admin_edit, :model_name => @model_name.snake_case, :id => @instance.id), :message => {:notice => "#{@model_name} was successfully updated"} elsif params[:_add_another] redirect slice_url(:admin_new, :model_name => @model_name.snake_case), :message => {:notice => "#{@model_name} was successfully updated"} else redirect slice_url(:admin_list, :model_name => @model_name.snake_case), :message => {:notice => "#{@model_name} was successfully updated"} end else [:error] = "#{@model_name} failed to be updated" render(:edit, :layout => "form") end end |