Class: MerbAdmin::Forms

Inherits:
Application show all
Defined in:
app/controllers/forms.rb

Instance Method Summary collapse

Methods inherited from Application

#set_model

Instance Method Details

#createObject



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
    message[:error] = "#{@model_name} failed to be created"
    render(:new, :layout => "form")
  end
end

#delete(id) ⇒ Object

Raises:

  • (NotFound)


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

Raises:

  • (NotFound)


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

Raises:

  • (NotFound)


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

#indexObject



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

#listObject



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
  options = {}
  filters = params[:filter] || {}
  filters.each_pair do |key, value|
    if @model.properties[key].primitive.to_s == "TrueClass"
      options.merge!(key.to_sym => (value == "true" ? true : false))
    elsif @model.properties[key].primitive.to_s == "Integer" && @model.properties[key].type.respond_to?(:flag_map)
      options.merge!(key.to_sym => value.to_sym)
    end
  end
  if params[:all]
    options = {
      :limit => 200,
    }.merge(options)
    @instances = @model.all(options).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 "))
      options.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
    options = {
      :page => @current_page,
      :per_page => 100,
    }.merge(options)
    @page_count, @instances = @model.paginated(options)
    options.delete(:page)
    options.delete(:per_page)
  end
  @record_count = @model.count(options)
  render(:layout => "list")
end

#newObject



53
54
55
56
# File 'app/controllers/forms.rb', line 53

def new
  @instance = @model.new
  render(:layout => "form")
end

#update(id) ⇒ Object

Raises:

  • (NotFound)


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
    message[:error] = "#{@model_name} failed to be updated"
    render(:edit, :layout => "form")
  end
end