Browse files

more refactoring out of namespace

  • Loading branch information...
1 parent 00fca2f commit 24ccab9f97ac3ee50b65fd92a8e837239d1e6037 @iain committed Jul 19, 2009
View
1 .gitignore
@@ -1 +1,2 @@
.DS_Store
+app/controllers/application_controller.rb
View
77 app/controllers/manage_controller.rb
@@ -1,77 +0,0 @@
-class RootTable::ManageController < ApplicationController
-
- before_filter :find_object_by_id
-
- def index
- @collection = model.all
- end
-
- def new
- @object = model.new
- end
-
- def sort
- params[table.underscore].each_with_index do |id, index|
- model.update_all(["#{root_table.order}=?", index+1], ['id=?', id])
- end
- render :nothing => true
- end
-
- def create
- @object = model.new(params[table.underscore])
- if @object.save
- flash[:notice] = I18n.t(:created, :table => model.human_name, :scope => :root_table)
- redirect_to root_table_table_manage_index_url(table)
- else
- render :new
- end
- end
-
- def edit
- end
-
- def update
- if @object.update_attributes(params[table.underscore])
- flash[:notice] = I18n.t(:updated, :table => model.human_name, :scope => :root_table)
- redirect_to root_table_table_manage_index_url(table)
- else
- render :edit
- end
- end
-
- def destroy
- @object.destroy
- flash[:notice] = I18n.t(:destroyed, :table => model.human_name, :scope => :root_table)
- redirect_to root_table_table_manage_index_url(table)
- end
-
- private
-
- def find_object_by_id
- @object = model.find(params[:id]) if params[:id]
- end
-
- def root_table
- ::ActiveRecord::Base.root_tables[table].first
- end
- helper_method :root_table
-
- def table
- @table ||= params[:table_id]
- end
- helper_method :table
-
- def model
- @model ||= table.to_s.camelize.constantize
- end
- helper_method :model
-
- def columns
- return @columns if @columns
- @columns = model.column_names - %w[ id updated_at created_at ]
- @columns -= [ root_table.order.to_s ] if root_table.acts_as_list?
- @columns
- end
- helper_method :columns
-
-end
View
51 app/controllers/root_table_contents_controller.rb
@@ -0,0 +1,51 @@
+class RootTableContentsController < ApplicationController
+
+ include RootTable::ActionController
+
+ before_filter :find_object_by_id
+
+ def index
+ @collection = model.all
+ end
+
+ def new
+ @object = model.new
+ end
+
+ def create
+ @object = model.new(params[table.underscore])
+ if @object.save
+ flash[:notice] = flash_messange(:created)
+ redirect_to root_table_root_table_contents_url(table)
+ else
+ render :new
+ end
+ end
+
+ def edit
+ end
+
+ def update
+ if @object.update_attributes(params[table.underscore])
+ flash[:notice] = flash_message(:updated)
+ redirect_to root_table_root_table_contents_url(table)
+ else
+ render :edit
+ end
+ end
+
+ def destroy
+ @object.destroy
+ flash[:notice] = flash_message(:destroyed)
+ redirect_to root_table_root_table_contents_url(table)
+ end
+
+ def sort
+ params[table.underscore].each_with_index do |id, index|
+ model.update_all(["#{root_table.order}=?", index+1], ['id=?', id])
+ end
+ render :nothing => true
+ end
+
+
+end
View
2 app/controllers/tables_controller.rb → app/controllers/root_tables_controller.rb
@@ -1,4 +1,4 @@
-class RootTable::TablesController < ApplicationController
+class RootTablesController < ApplicationController
def index
@tables = ActiveRecord::Base.all_root_tables
View
0 app/views/manage/_form.html.haml → ...views/root_table_contents/_form.html.haml
File renamed without changes.
View
2 app/views/manage/edit.html.haml → app/views/root_table_contents/edit.html.haml
@@ -1,4 +1,4 @@
%h2= rt(:edit_header, :table => model.human_name)
-- form_for @object, :url => root_table_table_manage_path(table) do |f|
+- form_for @object, :url => root_table_root_table_content_path(table) do |f|
= render :partial => "form", :locals => { :f => f }
View
6 app/views/manage/index.html.haml → ...views/root_table_contents/index.html.haml
@@ -17,10 +17,10 @@
= link_to_delete_root_table_item(table, object.id)
- if root_table.acts_as_list?
- = sortable_element table.underscore, :url => sort_root_table_table_manage_path, :handle => "handle"
+ = sortable_element table.underscore, :url => sort_root_table_root_table_contents_path, :handle => "handle"
%h3= rt(:actions)
%ul.actions
- %li= link_to rt(:new, :table => model.human_name), new_root_table_table_manage_path(table)
- %li= link_to rt(:back), root_table_tables_path
+ %li= link_to rt(:new, :table => model.human_name), new_root_table_root_table_content_path(table)
+ %li= link_to rt(:back), root_tables_path
View
2 app/views/manage/new.html.haml → app/views/root_table_contents/new.html.haml
@@ -1,4 +1,4 @@
%h2= rt(:new_header, :table => model.human_name)
-- form_for @object, :url => root_table_table_manage_index_path(table) do |f|
+- form_for @object, :url => root_table_root_table_contents_path(table) do |f|
= render :partial => "form", :locals => { :f => f }
View
0 app/views/tables/index.html.haml → app/views/root_tables/index.html.haml
File renamed without changes.
View
2 config/routes.rb
@@ -1,5 +1,5 @@
ActionController::Routing::Routes.draw do |map|
map.resources :root_tables, :only => :index do |p|
- p.resources :manage, :collection => {:sort => :post}
+ p.resources :root_table_contents, :collection => {:sort => :post}, :as => "contents"
end
end
View
39 lib/root_table.rb
@@ -133,5 +133,44 @@ def opt?(option)
end
+ module ActionController
+
+ def self.included(controller)
+ controller.helper_method :root_table
+ controller.helper_method :table
+ controller.helper_method :model
+ controller.helper_method :columns
+ end
+
+ private
+
+ def find_object_by_id
+ @object = model.find(params[:id]) if params[:id]
+ end
+
+ def root_table
+ ::ActiveRecord::Base.root_tables[table].first
+ end
+
+ def table
+ @table ||= params[:table_id]
+ end
+
+ def model
+ @model ||= table.to_s.camelize.constantize
+ end
+
+ def columns
+ return @columns if @columns
+ @columns = model.column_names - %w[ id updated_at created_at ]
+ @columns -= [ root_table.order.to_s ] if root_table.acts_as_list?
+ @columns
+ end
+
+ def flash_message(key)
+ I18n.t(key, :table => model.human_name, :scope => :root_table)
+ end
+
+ end
end

0 comments on commit 24ccab9

Please sign in to comment.