Class: EngineRoom::ContentController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/engine_room/content_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/engine_room/content_controller.rb', line 59

def create
  @element = @model.new(params[@section.model_name])
  if @element.save
    flash[:notice] = "#{@section.model_name.humanize} was successfully created."
    redirect_to_or_back :action => :index
  else
    add_crumb "New #{@section.model_name.humanize}"
    render :action => :new
  end
end

#destroyObject



86
87
88
89
90
91
92
# File 'app/controllers/engine_room/content_controller.rb', line 86

def destroy
  @element = @model.find(params[:id])
  @element.destroy

  flash[:notice] = "#{@section.model_name.humanize} was successfully deleted."
  redirect_to_or_back :action => :index
end

#editObject



70
71
72
73
# File 'app/controllers/engine_room/content_controller.rb', line 70

def edit
  @element = @model.find(params[:id])
  add_crumb "Edit #{@section.model_name.humanize}"
end

#indexObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/engine_room/content_controller.rb', line 40

def index
  if @section.view_type == 'detail'
    @element = @model.where(@section.condition).first
    if @element
      render :action => :edit
    else
      flash[:alert] = "No matching item could be found for section \"#{@section.name.titleize}\"."
      redirect_to :action => :overview
    end
  else
    @elements = @model.order(sort_column + " " + sort_direction).paginate :page => params[:page], :per_page => 20
  end
end

#init_sectionObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/engine_room/content_controller.rb', line 12

def init_section
  @sections = Section.all
  @layout = 'columns'
  @section = Section.find_by_name(params[:section_name])
  if @section.nil?
    flash[:alert] = "No section named #{params[:section_name]} was found."
    redirect_to :action => :overview
  else
    @model = get_model(@section.model_name) # TODO: catch exception
    if params[:bt_section] && params[:bt_id]
      bt_section = Section.find_by_name(params[:bt_section])
      if params.has_key?(@section.model_name) && @section.name != bt_section.name
        # setup foreign key for has_many relationships
        params[@section.model_name]["#{bt_section.model_name.singularize}_id"] = params[:bt_id]
      end
      add_crumb bt_section.name.titleize, {:controller => 'engine_room/content', :section_name => bt_section.name, :action => :index}
      add_crumb params[:bt_id], {:controller => 'engine_room/content', :section_name => bt_section.name, :id => params[:bt_id], :action => :edit}
    else
      add_crumb @section.name.titleize, {:controller => 'engine_room/content', :action => :index, :section_name => @section.name}
    end
  end
end

#newObject



54
55
56
57
# File 'app/controllers/engine_room/content_controller.rb', line 54

def new
  @element = @model.new
  add_crumb "New #{@section.model_name.humanize}"
end

#overviewObject



35
36
37
38
# File 'app/controllers/engine_room/content_controller.rb', line 35

def overview
  @sections = Section.all
  @layout = 'columns'
end

#updateObject



75
76
77
78
79
80
81
82
83
84
# File 'app/controllers/engine_room/content_controller.rb', line 75

def update
  @element = @model.find(params[:id])
  if @element.update_attributes(params[@section.model_name])
    flash[:notice] = "#{@section.model_name.humanize} was successfully updated."
    redirect_to_or_back :action => :index
  else
    add_crumb "Edit #{@section.model_name.humanize}"
    render :action => :edit
  end
end