Class: Admin::MasterController
- Includes:
- SslRequirement, Typus::Authentication, Typus::Format, Typus::Locale, Typus::Reloader
- Defined in:
- app/controllers/admin/master_controller.rb
Instance Method Summary collapse
-
#create ⇒ Object
Create new items.
- #destroy ⇒ Object
- #edit ⇒ Object
-
#index ⇒ Object
This is the main index of the model.
- #new ⇒ Object
-
#position ⇒ Object
Change item position.
-
#relate ⇒ Object
Relate a model object to another, this action is used only by the has_and_belongs_to_many relationships.
- #show ⇒ Object
- #toggle ⇒ Object
-
#unrelate ⇒ Object
Remove relationship between models.
- #update ⇒ Object
Methods included from Typus::Reloader
Methods included from Typus::Locale
Instance Method Details
#create ⇒ Object
Create new items. There’s an special case when we create an item from another item. In this case, after the item is created we also create the relationship between these items.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'app/controllers/admin/master_controller.rb', line 86 def create @item = @resource[:class].new(params[:item]) if @item.attributes.include?(Typus.user_fk) @item.attributes = { Typus.user_fk => session[:typus_user_id] } end if @item.valid? create_with_back_to and return if params[:back_to] @item.save flash[:success] = _("{{model}} successfully created.", :model => @resource[:class].typus_human_name) if @resource[:class].(:index_after_save) redirect_to :action => 'index' else redirect_to :action => @resource[:class].(:default_action_on_item), :id => @item.id end else select_template :new end end |
#destroy ⇒ Object
147 148 149 150 151 152 153 |
# File 'app/controllers/admin/master_controller.rb', line 147 def destroy @item.destroy flash[:success] = _("{{model}} successfully removed.", :model => @resource[:class].typus_human_name) redirect_to :back rescue Exception => error error_handler(error, params.merge(:action => 'index', :id => nil)) end |
#edit ⇒ Object
109 110 111 112 113 114 115 116 |
# File 'app/controllers/admin/master_controller.rb', line 109 def edit item_params = params.dup %w( action controller model model_id back_to id resource resource_id page ).each { |p| item_params.delete(p) } # We assign the params passed trough the url @item.attributes = item_params @previous, @next = @item.previous_and_next(item_params) select_template :edit end |
#index ⇒ Object
This is the main index of the model. With filters, conditions and more.
By default application can respond_to html, csv and xml, but you can add your formats.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'app/controllers/admin/master_controller.rb', line 51 def index @conditions, @joins = @resource[:class].build_conditions(params) check_ownership_of_items if @resource[:class].(:only_user_items) respond_to do |format| format.html { generate_html } @resource[:class].typus_export_formats.each do |f| format.send(f) { send("generate_#{f}") } end end rescue Exception => error error_handler(error) end |
#new ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'app/controllers/admin/master_controller.rb', line 68 def new item_params = params.dup %w( controller action resource resource_id back_to selected ).each do |param| item_params.delete(param) end @item = @resource[:class].new(item_params.symbolize_keys) select_template :new end |
#position ⇒ Object
Change item position. This only works if acts_as_list is installed. We can then move items:
params[:go] = 'move_to_top'
Available positions are move_to_top, move_higher, move_lower, move_to_bottom.
176 177 178 179 180 |
# File 'app/controllers/admin/master_controller.rb', line 176 def position @item.send(params[:go]) flash[:success] = _("Record moved {{to}}.", :to => params[:go].gsub(/move_/, '').humanize.downcase) redirect_to :back end |
#relate ⇒ Object
Relate a model object to another, this action is used only by the has_and_belongs_to_many relationships.
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'app/controllers/admin/master_controller.rb', line 186 def relate resource_class = params[:related][:model].constantize resource_tableized = params[:related][:model].tableize @item.send(resource_tableized) << resource_class.find(params[:related][:id]) flash[:success] = _("{{model_a}} related to {{model_b}}.", :model_a => resource_class.typus_human_name, :model_b => @resource[:class].typus_human_name) redirect_to :action => @resource[:class].(:default_action_on_item), :id => @item.id, :anchor => resource_tableized end |
#show ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'app/controllers/admin/master_controller.rb', line 118 def show @previous, @next = @item.previous_and_next respond_to do |format| format.html { select_template :show } format.xml do fields = @resource[:class].typus_fields_for(:xml).collect { |i| i.first } render :xml => @item.to_xml(:only => fields) end end end |
#toggle ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 |
# File 'app/controllers/admin/master_controller.rb', line 155 def toggle if @resource[:class].(:toggle) @item.toggle!(params[:field]) flash[:success] = _("{{model}} {{attribute}} changed.", :model => @resource[:class].typus_human_name, :attribute => params[:field].humanize.downcase) else flash[:notice] = _("Toggle is disabled.") end redirect_to :back end |
#unrelate ⇒ Object
Remove relationship between models.
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'app/controllers/admin/master_controller.rb', line 206 def unrelate resource_class = params[:resource].classify.constantize resource = resource_class.find(params[:resource_id]) case params[:association] when 'has_and_belongs_to_many' @item.send(resource_class.table_name).delete(resource) = "{{model_a}} unrelated from {{model_b}}." when 'has_many', 'has_one' resource.destroy = "{{model_a}} removed from {{model_b}}." end flash[:success] = _(, :model_a => resource_class.typus_human_name, :model_b => @resource[:class].typus_human_name) redirect_to :controller => @resource[:self], :action => @resource[:class].(:default_action_on_item), :id => @item.id, :anchor => resource_class.table_name end |
#update ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'app/controllers/admin/master_controller.rb', line 132 def update if @item.update_attributes(params[:item]) flash[:success] = _("{{model}} successfully updated.", :model => @resource[:class].typus_human_name) path = if @resource[:class].(:index_after_save) params[:back_to] ? "#{params[:back_to]}##{@resource[:self]}" : { :action => 'index' } else { :action => @resource[:class].(:default_action_on_item), :id => @item.id, :back_to => params[:back_to] } end redirect_to path else @previous, @next = @item.previous_and_next select_template :edit end end |