Module: CropImageRiffpad::ControllerMethod

Defined in:
lib/cropimage_riffpad/crop_image_controller.rb

Instance Method Summary collapse

Instance Method Details

#crop_image(model_name, attachment_name, style) ⇒ Object

model_name = “user” => class User | model_name = “user_photo” => UserPhoto



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cropimage_riffpad/crop_image_controller.rb', line 4

def crop_image(model_name, attachment_name, style) # model_name = "user" => class User | model_name = "user_photo" => UserPhoto
  [:update, :create].each do |action_name|
    define_method action_name do # define action update for controller
      if params[:action].to_s == "update"
        @object = ActiveSupport::Inflector.constantize(ActiveSupport::Inflector.classify(model_name)).find(params[:id])
        @object.attributes = params[model_name.to_sym]
      else
        @object = ActiveSupport::Inflector.constantize(ActiveSupport::Inflector.classify(model_name)).new(params[model_name.to_sym])                      
      end
      if @object.save
        params[:id] = @object.id
        if params[:crop] == "true"
          redirect_to self.send("#{model_name}_url", @object)
        else
          if params[model_name.to_sym] and params[model_name.to_sym][attachment_name.to_sym].blank?
            responds_to_parent do
              render :js => "document.location = \"#{escape_javascript(self.send("#{model_name}_url", @object))}\""
            end
          else
            responds_to_parent do 
              crop_action_content = render_to_string :action => :crop
              render :js => "renderFancyBoxWithContent('#{escape_javascript(crop_action_content)}');"
            end
          end
        end
      else
        edit_action_content = render_to_string :action => params[:action]
        responds_to_parent do 
          render :js => "$('#container').html('#{escape_javascript(edit_action_content)}');"
        end
      end
    end
  end
end