Class: ActionController::FirstFloor

Inherits:
Base
  • Object
show all
Defined in:
lib/action_controller/first_floor.rb

Instance Method Summary collapse

Instance Method Details

#createObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/action_controller/first_floor.rb', line 22

def create
  obj_class = controller_name.to_s.singularize.titleize
  obj_name  = controller_name.to_s.singularize
  @obj = obj_class.constantize.new(params[obj_name.to_sym])
  if @obj.save
    flash[:notice] = "#{obj_class} was successfully created"
    flash[:status] = 201
    redirect_to :action => :index
  else
    eval( "@#{obj_name} = @obj" )

    flash[:alert] = "Cannot create this #{obj_class}. There were some errors."
    flash[:notice] = flash[:alert]
    flash[:status] = 400
    render_response( eval( "@#{obj_name}" ), 'new', 400 )
  end
end

#destroyObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/action_controller/first_floor.rb', line 69

def destroy
  obj_class = controller_name.to_s.singularize.titleize
  obj       = obj_class.constantize.find(params[:id])
  obj_info  = nil

  if obj.respond_to?(:name)
    obj_info = "#{obj.id}:#{obj.name}"
  else
    obj_info = "#{obj.id}:#{obj.to_s}"
  end
  obj_class.constantize.delete(params[:id])
  flash[:notice] = "#{obj_class} '#{obj_info} successfully deleted.'"
  redirect_to :action => 'index'
end

#editObject



40
41
42
43
44
45
# File 'lib/action_controller/first_floor.rb', line 40

def edit
  obj_class = controller_name.to_s.singularize.titleize
  view_obj  = controller_name.to_s.singularize

  eval( "@#{view_obj} = obj_class.constantize.find(params[:id])" )
end

#indexObject



2
3
4
5
6
7
# File 'lib/action_controller/first_floor.rb', line 2

def index
  obj_class = controller_name.to_s.singularize.titleize
  view_obj  = controller_name.to_s
  eval( "@#{view_obj} = obj_class.constantize.find(:all)" )
  render_response( eval( "@#{view_obj}" ), 'index' )
end

#newObject



16
17
18
19
20
# File 'lib/action_controller/first_floor.rb', line 16

def new
  obj_class = controller_name.to_s.singularize.titleize
  view_obj  = controller_name.to_s.singularize
  eval( "@#{view_obj} = obj_class.constantize.new" )
end

#render_response(obj, tmpl, status = :ok) ⇒ Object



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
# File 'lib/action_controller/first_floor.rb', line 84

def render_response(obj, tmpl, status = :ok)
  klass = nil
  if obj.class == Array
    klass = obj[0].class

    ## Make sure we're not dealing with a NilClass, which we don't have
    ## a template for. Default to the controller class.
    if klass == NilClass
      klass = controller_name.to_s.singularize.titleize
    end
  else
    klass = obj.class
  end

  namespace = klass.to_s.downcase.pluralize

  if /^Admin/.match( self.class.to_s )
    namespace = "admin/#{namespace}"
  end

  respond_to do |format|
    format.html { render :template => "#{namespace}/#{tmpl}",
                                               :status => status }
    format.xml  { render :xml  => obj.to_xml,  :status => status }
    format.json { render :json => obj.to_json, :status => status }
    format.yaml { render :text => obj.to_yaml, :status => status }
  end
end

#showObject



9
10
11
12
13
14
# File 'lib/action_controller/first_floor.rb', line 9

def show
  obj_class = controller_name.to_s.singularize.titleize
  view_obj  = controller_name.to_s.singularize
  eval( "@#{view_obj} = obj_class.constantize.find(params[:id])" )
  render_response( eval( "@#{view_obj}" ) , 'show' )
end

#updateObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/action_controller/first_floor.rb', line 47

def update
  obj_class = controller_name.to_s.singularize.titleize
  obj_name  = controller_name.to_s.singularize
  @obj      = obj_class.constantize.find(params[:id])

  if @obj.update_attributes( params[obj_name.to_sym] )
    check_validation(@obj)

    flash[:notice] = "#{obj_class} was successfully updated."
    flash[:status] = 200
    eval( "@#{obj_name} = @obj" )
    redirect_to :action => :show, :id => eval( "@#{obj_name}" )
  else
    eval( "@#{obj_name} = @obj" )

    flash[:alert] = "#{obj_class} had some errors and was not updated."
    flash[:notice] = flash[:alert]
    flash[:status] = 400
    render_response( eval( "@#{obj_name}" ), 'edit', 400 )
  end
end