Class: Admin::ResourcesController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/admin/resources_controller.rb

Overview

@Name: Admin Resources controller @Use: Creating , modifying, deleting resources for the cms @Created date: 24-07-2012 @Modified Date: 24-07-2012

@Company:  Mindfire Solutions

Instance Method Summary collapse

Instance Method Details

#createObject

POST /admin/images This method creates file record and upload resources to the local server



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/controllers/admin/resources_controller.rb', line 82

def create
  @resource = Resource.new(params[:resource])

  @resource.assetable_id = 1
  @resource.assetable_type = 'User'

  if params[:resource]
    if image_types.include? params[:resource][:data].content_type
      @resource.type = 'Ckeditor::Picture'
    else
      @resource.type = 'Ckeditor::AttachmentFile'
    end
  end

  respond_to do |format|
    
    if @resource.save
      
      #Checking resource type for image or file and redirecting as per the lists with conditions
      if @resource.is_type_image?        
        redirect_path = admin_resources_path(:type => 'image')
      else
        redirect_path = admin_resources_path(:type => 'file')
      end

      format.html { redirect_to redirect_path, notice: 'Resource was successfully uploaded.' }
    else
      format.html { render action: "new", :type => @type }
    end
  end
end

#destroyObject

DELETE /admin/images/1 This method destroys file records



142
143
144
145
146
147
148
149
150
# File 'app/controllers/admin/resources_controller.rb', line 142

def destroy
  @resource = Resource.find(params[:id])
  @resource.destroy

  respond_to do |format|
    format.html { redirect_to admin_resources_url }
    format.json { head :no_content }
  end
end

#editObject

GET /admin/images/1/edit This method opens the edit form for file upload



72
73
74
75
76
77
78
# File 'app/controllers/admin/resources_controller.rb', line 72

def edit

  @resource = Resource.find(params[:id])
  @resource.type = 'Resource'
  @type = 'file' if params[:type] == 'file'

end

#indexObject

GET /admin/images This is a default method for listing all assets for mcms_resources



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/admin/resources_controller.rb', line 21

def index

  #checking params type whether file or image and the perticaular file types will be sorted
  @type = params[:type]

  if @type == 'image'
    @resources = Resource.all_images.paginate({
      :page => params[:page],
      :per_page => resource_per_page
    })
  elsif @type == 'file'
    @resources = Resource.all_files.paginate({
      :page => params[:page],
      :per_page => resource_per_page
    })
  else
    @resources = Resource.paginate({
      :page => params[:page],
      :per_page => resource_per_page
    })
  end

  respond_to do |format|
    format.html{}
  end
end

#newObject

GET /admin/images/new This is method for opening form for uploading a resource Checks the type of resource needs to be uploaded and open the file field



62
63
64
65
66
67
68
# File 'app/controllers/admin/resources_controller.rb', line 62

def new
  @resource = Resource.new
  @type = 'file' if params[:type] == 'file'
  respond_to do |format|
    format.html{}
  end
end

#showObject

GET /admin/images/1 This is a method for showing an image



50
51
52
53
54
55
56
57
# File 'app/controllers/admin/resources_controller.rb', line 50

def show
  @resource = Resource.find(params[:id]) || not_found

  respond_to do |format|
    format.html{}
  end

end

#updateObject

PUT /admin/resources/1 This method updates file record and upload resources to the local server



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/controllers/admin/resources_controller.rb', line 116

def update
  @resource = Resource.find(params[:id])

  @resource.assetable_id = 1
  @resource.assetable_type = 'User'

  if image_types.include? params[:resource][:data].content_type
    @resource.type = 'Ckeditor::Picture'
  else
    @resource.type = 'Ckeditor::AttachmentFile'
  end

  respond_to do |format|
    if @resource.update_attributes(params[:resource])
      format.html { redirect_to admin_resources_path, notice: 'Resource was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: "edit" }
      format.json { render json: @resource.errors, status: :unprocessable_entity }
    end
  end

end