Module: Toolbox::DefaultController::InstanceMethods

Defined in:
lib/toolbox/default_controller.rb

Overview

:nodoc:all

Instance Method Summary collapse

Instance Method Details

#context_menuObject



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/toolbox/default_controller.rb', line 250

def context_menu
  rec = model_class.find(params[:id])
  set_record rec
  context = {}
  params.select { |k,v| k.ends_with? '_id' }.each {|a| context[a[0]] = a[1] }
  locals = { :context => context, :widget_list => menu_config(rec, context)}
  respond_to do |format|
    format.html { render :partial => "toolbox/context_menu", :object => rec, :locals => locals  }
    format.js do
      render :update do |page|
        page.replace_html  'context_menu_id', :partial => "toolbox/context_menu", :object => rec, :locals => locals
      end
    end
  end
end

#createObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/toolbox/default_controller.rb', line 154

def create
  if params[:save] # save or ok clicked
    p = params[model_name.to_sym]
    preprocess_params nil, p
    obj = model_class.new p
    set_record obj

    respond_to do |format|
      if obj.save
        obj = redirect_create obj if respond_to? :redirect_create
        flash[:notice] = Messages.created % { :object => s_(controller_name.singularize.humanize.downcase)}
        format.html { redirect_to(obj) }
        format.js do
          render :update do
            |page| page << 'window.location.reload();'  # TODO improvment?
          end
        end
        format.xml  { render :xml => obj, :status => :created, :location => obj }
      else
        format.html { render :template => "toolbox/new" }
        format.xml  { render :xml => obj.errors, :status => :unprocessable_entity }
        format.js do
          render :update do |page|
            locals = {:widgetsets => controller.edit_config(obj).controlsets, :dialog => true }
            page.replace_html  'dialog_id', :partial => "toolbox/form", :object => obj, :locals => locals
          end
        end
      end
    end
  else # cancel clicked
    redirect_to eval("#{controller_name}_path")
  end
end

#destroyObject



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/toolbox/default_controller.rb', line 234

def destroy
  obj = model_class.find(params[:id])
  set_record obj
  obj.destroy

  respond_to do |format|
    format.html { redirect_to(send(controller_name + '_url')) }
    format.xml  { head :ok }
    format.js do
      render :update do |page|
        page << 'window.location.reload();' # TODO: improve?
      end
    end
  end
end

#editObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/toolbox/default_controller.rb', line 138

def edit
  obj = model_class.find(params[:id])
  set_record obj

  respond_to do |format|
    format.html { render :template => 'toolbox/edit' }
    format.js do
      render :update do |page|
        locals = {:widgetsets => controller.edit_config(obj).controlsets, :dialog => true }
        page.replace_html  'dialog_id', :partial => "toolbox/form", :object => obj, :locals => locals
        page << "$('dialog_id').popup.show();"
      end
    end
  end
end

#embedded_list_collection(rec) ⇒ Object



274
275
276
277
278
279
# File 'lib/toolbox/default_controller.rb', line 274

def embedded_list_collection rec
  options = {}
  options[:order] = Toolbox::Sorting.order_by params, @embedded_list.widget_list.widgets, @embedded_list.prefix
  options[:include] = @embedded_list.include_assoc if @embedded_list.include_assoc
  rec.send(@embedded_list.collection_config.model_method).find(:all, options)
end

#indexObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/toolbox/default_controller.rb', line 64

def index
  obj = paging
  instance_variable_set :@recs, obj

  respond_to do |format|
    format.html  do
      begin
        render # allow to overwrite the view of an action
      rescue  ActionView::MissingTemplate
        render :template => 'toolbox/index'
      end
    end
    format.xml  { render :xml => obj }
  end
end

#model_classObject



270
271
272
# File 'lib/toolbox/default_controller.rb', line 270

def model_class
  controller_name.classify.constantize
end

#model_nameObject



266
267
268
# File 'lib/toolbox/default_controller.rb', line 266

def model_name
  controller_name.singularize
end

#newObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/toolbox/default_controller.rb', line 108

def new
  # Handle dublicate command
  param_name = "#{model_name}_id".to_sym
  attr = {}
  attr = model_class.find(params[param_name]).attributes if params[param_name]
  obj = model_class.new attr

  set_record obj

  # context support
  # set all _id fields, if available from params
  params.each_pair do | key,value |
    if key.to_s.ends_with? '_id' and obj.respond_to? key + '='
      obj.send(key + '=', value)
    end
  end

  respond_to do |format|
    format.html { render :template => 'toolbox/new' }
    format.xml  { render :xml => obj }
    format.js do
      render :update do |page|
        locals = {:widgetsets => controller.edit_config(obj).controlsets, :dialog => true }
        page.replace_html  'dialog_id', :partial => "toolbox/form", :object => obj, :locals => locals
        page << "$('dialog_id').popup.show();"
      end
    end
  end
end

#showObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/toolbox/default_controller.rb', line 80

def show
  rec = model_class.find(params[:id])
  set_record rec

  @show_config = show_config rec
  @embedded_list = nil
  if @show_config.embedded_lists.length > 0
    prefix = params[:list] || @show_config.embedded_lists.first.prefix
    @show_config.embedded_lists.each do |embedded_list|
      @embedded_list = embedded_list if embedded_list.prefix == prefix
    end
  end

  respond_to do |format|
    format.html do
      begin
        render # allow to overwrite the view of an action
      rescue ActionView::MissingTemplate
        render :template => 'toolbox/show'
      end
    end
    format.xml  { render :xml => rec }
    format.pdf #{ render @show_config.pdf_options }
    format.csv  { stream_csv show_csv(rec, params) }
  end
end

#updateObject



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/toolbox/default_controller.rb', line 189

def update
  obj = model_class.find(params[:id])
  set_record obj

  if params[:save]
    par = params[model_name.to_sym]
    preprocess_params obj, par
    # Ensure that the the collection attributes exist - create them if not
    # This is needed, if all collection items have been deleted.
    edit_config(obj).controlsets.each do |controlset|
      if controlset.collection_config
        attr = Toolbox::Helpers.collection_attribute_name(controlset.collection_config, obj)
        par[attr] ||= {}
      end
    end

    respond_to do |format|
      if obj.update_attributes(par)
        flash[:notice] = Messages.updated % { :object => s_(controller_name.singularize.humanize.downcase)}
        format.html { redirect_to(obj) }
        format.xml  { head :ok }
        format.js do
          render :update do |page|
            page << 'window.location.reload();'  # TODO improve ?
          end
        end
      else
        format.html { render :template => "toolbox/edit" }
        format.xml  { render :xml => obj.errors, :status => :unprocessable_entity }
        format.js do
          render :update do |page|
            locals = {:widgetsets => controller.edit_config(obj).controlsets, :dialog => true }
            page.replace_html  'dialog_id', :partial => "toolbox/form", :object => obj, :locals => locals
          end
        end
      end
    end
  else # cancel clicked
    respond_to do |format|
      format.html { redirect_to(obj) }
      format.xml  { head :ok }
    end
  end
end