Module: QM::ActsAsGenericController::ControllerIncludes::InstanceMethods

Defined in:
lib/qm-acts-as-generic-controller-controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/qm-acts-as-generic-controller-controller.rb', line 94

def create
  model.transaction do
    remove_unprivileged_keys_from_params

    instance_variable_set(singular_variable, model.new(params[singular_variable(true)])) unless instance_variable_defined?(singular_variable)
    
    save_creator
    save_updator
    

    respond_to do |format|
      if instance_variable_get(singular_variable).save
        flash[:notice] = :savedAsFlash
        if defined?(section)
          format.html { 
            if params[:commit_and_create] 
              if params[:association_attribute_name] and params[:association_attribute_value]
                redirect_to polymorphic_url([ "new", section, model.table_name.singularize ], :association_attribute => params[:association_attribute_name], params[:association_attribute_name] => params[:association_attribute_value])
              else
                redirect_to polymorphic_url([ "new", section, model.table_name.singularize ])
              
              end
            else
              redirect_to [ section, instance_variable_get(singular_variable) ] 
            end
          }
          
          format.xml  { render :xml => instance_variable_get(singular_variable).to_xml(:only => current_user_xml_privileged_attributes), :status => :created, :location => [ section, instance_variable_get(singular_variable) ] }


        else
          format.html { 
            if params[:commit_and_create] 
              if params[:association_attribute_name] and params[:association_attribute_value]
                redirect_to polymorphic_url([ "new", model.table_name.singularize ], :association_attribute => params[:association_attribute_name], params[:association_attribute_name] => params[:association_attribute_value])
              else
                redirect_to polymorphic_url([ "new", model.table_name.singularize ])
              end
              
            else
              redirect_to instance_variable_get(singular_variable) 
            end

          }
          
          format.xml { 
            if defined?(current_user) and current_user.respond_to? :privileged_attributes
              render :xml => instance_variable_get(singular_variable).to_xml(:only => current_user_xml_privileged_attributes), :status => :created, :location => instance_variable_get(singular_variable)
            else
              render :xml => instance_variable_get(singular_variable).to_xml(:only => current_user_xml_privileged_attributes), :status => :created, :location => instance_variable_get(singular_variable)
            end
          }
        end
      else
        format.html { 
          begin
            render :action => "new"
          
          rescue ActionView::MissingTemplate
            render :template => "generic_controller/form" 
          end
        } 
        format.xml { 
          if defined?(current_user) and current_user.respond_to? :privileged_attributes
            render :xml => instance_variable_get(singular_variable).to_xml(:only => current_user_xml_privileged_attributes), :status => :unprocessable_entity
          else
            render :xml => instance_variable_get(singular_variable), :status => :unprocessable_entity
          end
        }
      end
    end
  end
end

#destroyObject



208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/qm-acts-as-generic-controller-controller.rb', line 208

def destroy
  instance_variable_set(singular_variable, model.find(params[:id])) unless instance_variable_defined?(singular_variable)

  check_generic_actions or return

  instance_variable_get(singular_variable).destroy

  flash[:notice] = :destroyedAsFlash

  respond_to do |format|
    format.html { redirect_to :action => "index" }
    format.xml  { head :ok }
  end
end

#editObject



72
73
74
75
76
# File 'lib/qm-acts-as-generic-controller-controller.rb', line 72

def edit
  instance_variable_set(singular_variable, model.find(params[:id])) unless instance_variable_defined?(singular_variable)

  check_generic_actions or return
end

#indexObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/qm-acts-as-generic-controller-controller.rb', line 37

def index
  unless instance_variable_defined?(plural_variable)
    instance_variable_set(plural_variable, model.limit_with_security_scheme(:user => current_user, :paginate => true, :paginate_page => params[:page] || 1, :paginate_per_page => params[:per_page] || 100, :sort_by => params[:sort_by], :sort_order => params[:sort_order])) 
  end
  
  respond_to do |format|
    format.html
    format.xml { 
      if defined?(current_user) and current_user.respond_to? :privileged_attributes
        render :xml => instance_variable_get(plural_variable).to_xml(:only => current_user_xml_privileged_attributes)
      else
        render :xml => instance_variable_get(plural_variable) 
      end
    }
  end
end

#newObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/qm-acts-as-generic-controller-controller.rb', line 78

def new
  instance_variable_set singular_variable, model.new(params[singular_variable(true)]) unless instance_variable_defined?(singular_variable)

  respond_to do |format|
    format.html
    format.xml { 
      if defined?(current_user) and current_user.respond_to? :privileged_attributes
        render :xml => instance_variable_get(singular_variable).to_xml(:only => current_user_xml_privileged_attributes)
      else
        render :xml => instance_variable_get(singular_variable)
      end
    }

  end
end

#showObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/qm-acts-as-generic-controller-controller.rb', line 54

def show
  instance_variable_set(singular_variable, model.find(params[:id])) unless instance_variable_defined?(singular_variable)

  check_generic_actions or return

  respond_to do |format|
    format.html 

    format.xml { 
      if defined?(current_user) and current_user.respond_to? :privileged_attributes
        render :xml => instance_variable_get(singular_variable).to_xml(:only => current_user_xml_privileged_attributes)
      else
        render :xml => instance_variable_get(singular_variable)
      end
    }
  end
end

#updateObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/qm-acts-as-generic-controller-controller.rb', line 168

def update
  instance_variable_set(singular_variable, model.find(params[:id])) unless instance_variable_defined?(singular_variable)

  check_generic_actions or return

  remove_unprivileged_keys_from_params

  instance_variable_get(singular_variable).update_attributes params[singular_variable(true)]
  save_updator

  respond_to do |format|
    if instance_variable_get(singular_variable).save
      flash[:notice] = :savedAsFlash
      if defined?(section)
        format.html { redirect_to [ section, instance_variable_get(singular_variable) ] }
        format.xml  { head :ok }
      else
        format.html { redirect_to instance_variable_get(singular_variable) }
        format.xml  { head :ok }
      end
    else
      format.html { 
        begin
          render :action => "new"
        
        rescue ActionView::MissingTemplate
          render :template => "generic_controller/form" 
        end
      } 
      format.xml { 
        if defined?(current_user) and current_user.respond_to? :privileged_attributes
          render :xml => instance_variable_get(singular_variable).to_xml(:only => current_user_xml_privileged_attributes), :status => :unprocessable_entity
        else
          render :xml => instance_variable_get(singular_variable), :status => :unprocessable_entity
        end
      }
    end
  end
end