admin file thumbnails

jamesperet лет %!s(int64=10): %!d(string=назад)
Родитель
Сommit
5e2978e286

+ 2 - 1
.gitignore

@@ -61,4 +61,5 @@ Temporary Items
61 61
 /config/application.yml
62 62
 
63 63
 # Ignore File Uploads
64
-/public/uploads
64
+/public/uploads
65
+/public/ .

+ 8 - 0
app/assets/stylesheets/admin_panel.css.less

@@ -1,3 +1,11 @@
1 1
 // Place all the styles related to the admin_panel controller here.
2 2
 // They will automatically be included in application.css.
3 3
 // You can use Less here: http://lesscss.org/
4
+
5
+body {
6
+	max-width:1577px;
7
+}
8
+
9
+.navbar-inner {
10
+	max-width:1577px;
11
+}

+ 2 - 0
app/controllers/admin_panel_controller.rb

@@ -1,5 +1,7 @@
1 1
 class AdminPanelController < ApplicationController
2 2
   
3
+  layout 'admin'
4
+  
3 5
   def index
4 6
     redirect_to admin_dashboard_path
5 7
   end

+ 0 - 6
app/controllers/blog_posts_controller.rb

@@ -64,13 +64,7 @@ class BlogPostsController < ApplicationController
64 64
     end
65 65
   end
66 66
   
67
-  def upload
68
-      @file = params[:file]
69
-      @upload = Upload.create(:file => @file)
70
-      @upload.save
71 67
 
72
-      render(json: {:url => @upload.file.to_s })
73
-  end
74 68
 
75 69
   private
76 70
     # Use callbacks to share common setup or constraints between actions.

+ 11 - 2
app/controllers/uploads_controller.rb

@@ -56,10 +56,19 @@ class UploadsController < ApplicationController
56 56
   def destroy
57 57
     @upload.destroy
58 58
     respond_to do |format|
59
-      format.html { redirect_to uploads_url }
59
+      format.html { redirect_to admin_files_path }
60 60
       format.json { head :no_content }
61 61
     end
62 62
   end
63
+  
64
+  def upload
65
+      @file = params[:file]
66
+      @upload = Upload.create(:file => @file)
67
+      @upload.title = @upload.file.to_s.split('/').last
68
+      @upload.save
69
+
70
+      render(json: {:url => @upload.file.to_s })
71
+  end
63 72
 
64 73
   private
65 74
     # Use callbacks to share common setup or constraints between actions.
@@ -69,6 +78,6 @@ class UploadsController < ApplicationController
69 78
 
70 79
     # Never trust parameters from the scary internet, only allow the white list through.
71 80
     def upload_params
72
-      params.require(:upload).permit(:title, :file, :desciption)
81
+      params.require(:upload).permit(:title, :file, :description)
73 82
     end
74 83
 end

+ 15 - 0
app/models/upload.rb

@@ -2,4 +2,19 @@ class Upload < ActiveRecord::Base
2 2
   
3 3
     mount_uploader :file, FileUploader
4 4
   
5
+    def get_extension
6
+      case self.file.file.content_type
7
+      when 'image/jpeg'
8
+        return 'jpg'
9
+      when 'image/png'
10
+        return 'png'
11
+      when 'image/gif'
12
+        return 'gif'
13
+      when 'application/pdf'
14
+        return 'pdf'
15
+      else
16
+        return 'none'
17
+      end
18
+    end
19
+  
5 20
 end

+ 9 - 3
app/uploaders/file_uploader.rb

@@ -49,9 +49,9 @@ class FileUploader < CarrierWave::Uploader::Base
49 49
   # end
50 50
 
51 51
   # Create different versions of your uploaded files:
52
-  # version :thumb do
53
-  #   process :resize_to_fit => [50, 50]
54
-  # end
52
+  version :thumb, :if => :image? do
53
+    process :resize_to_fill => [300, 200,  gravity = 'Center']
54
+  end
55 55
 
56 56
   # Add a white list of extensions which are allowed to be uploaded.
57 57
   # For images you might use something like this:
@@ -65,4 +65,10 @@ class FileUploader < CarrierWave::Uploader::Base
65 65
   #   "something.jpg" if original_filename
66 66
   # end
67 67
 
68
+  protected
69
+    
70
+    def image?(new_file)
71
+      new_file.content_type.start_with? 'image'
72
+    end
73
+
68 74
 end

+ 15 - 27
app/views/admin_panel/files.html.erb

@@ -4,32 +4,20 @@
4 4
 		<div class="page-header">
5 5
 		  <h1>Blog Posts <%= link_to 'Upload File', new_upload_path, :class => 'btn btn-primary btn-mini' %></h1>
6 6
 		</div>
7
-
8
-			<table class="table table-bordered">
9
-			  <thead>
10
-			    <tr>
11
-			      <th>Title</th>
12
-			      <th>File</th>
13
-			      <th>Desciption</th>
14
-			      <th></th>
15
-			      <th></th>
16
-			      <th></th>
17
-			    </tr>
18
-			  </thead>
19
-
20
-			  <tbody>
21
-			    <% @uploads.each do |upload| %>
22
-			      <tr>
23
-			        <td><%= upload.title %></td>
24
-			        <td><%= upload.file.to_s %></td>
25
-			        <td><%= upload.desciption %></td>
26
-			        <td><%= link_to 'Show', upload %></td>
27
-			        <td><%= link_to 'Edit', edit_upload_path(upload) %></td>
28
-			        <td><%= link_to 'Destroy', upload, method: :delete, data: { confirm: 'Are you sure?' } %></td>
29
-			      </tr>
30
-			    <% end %>
31
-			  </tbody>
32
-			</table>
33
-			
7
+		<ul class="thumbnails">
8
+		    <% @uploads.each do |upload| %>
9
+			   <li class="span3" style="">
10
+			     <div class="thumbnail">
11
+				  <% if upload.file.content_type.start_with?('image') && upload.file.thumb.to_s != nil %>
12
+				  	<%= image_tag upload.file.thumb.to_s %>
13
+				  <% else %>
14
+			       	<img src="http://placehold.it/300x200" alt="">
15
+				  <% end %>
16
+			       <h5 style="height: 40px"><%= link_to upload.title, upload_path(upload) %></h5>
17
+			       <p><%= upload.description %></p>
18
+			     </div>
19
+			   </li>
20
+		    <% end %>
21
+		</ul>
34 22
 	</div>
35 23
 </div>

+ 67 - 0
app/views/layouts/admin.html.erb

@@ -0,0 +1,67 @@
1
+<!DOCTYPE html>
2
+<html lang="en">
3
+  <head>
4
+    <meta charset="utf-8">
5
+    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
6
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
+    <title><%= content_for?(:title) ? yield(:title) : "RailsWebsiteTemplate" %></title>
8
+    <%= csrf_meta_tags %>
9
+
10
+    <!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
11
+    <!--[if lt IE 9]>
12
+      <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.6.1/html5shiv.js" type="text/javascript"></script>
13
+    <![endif]-->
14
+
15
+    <%= stylesheet_link_tag "application", :media => "all" %>
16
+
17
+    <!-- For third-generation iPad with high-resolution Retina display: -->
18
+    <!-- Size should be 144 x 144 pixels -->
19
+    <%= favicon_link_tag 'apple-touch-icon-144x144-precomposed.png', :rel => 'apple-touch-icon-precomposed', :type => 'image/png', :sizes => '144x144' %>
20
+
21
+    <!-- For iPhone with high-resolution Retina display: -->
22
+    <!-- Size should be 114 x 114 pixels -->
23
+    <%= favicon_link_tag 'apple-touch-icon-114x114-precomposed.png', :rel => 'apple-touch-icon-precomposed', :type => 'image/png', :sizes => '114x114' %>
24
+
25
+    <!-- For first- and second-generation iPad: -->
26
+    <!-- Size should be 72 x 72 pixels -->
27
+    <%= favicon_link_tag 'apple-touch-icon-72x72-precomposed.png', :rel => 'apple-touch-icon-precomposed', :type => 'image/png', :sizes => '72x72' %>
28
+
29
+    <!-- For non-Retina iPhone, iPod Touch, and Android 2.1+ devices: -->
30
+    <!-- Size should be 57 x 57 pixels -->
31
+    <%= favicon_link_tag 'apple-touch-icon-precomposed.png', :rel => 'apple-touch-icon-precomposed', :type => 'image/png' %>
32
+
33
+    <!-- For all other devices -->
34
+    <!-- Size should be 32 x 32 pixels -->
35
+    <%= favicon_link_tag 'favicon.ico', :rel => 'shortcut icon' %>
36
+
37
+    <%= javascript_include_tag "application" %>
38
+  </head>
39
+  <body>
40
+
41
+    <div class="navbar navbar-fixed-top">
42
+      <div class="navbar-inner">
43
+        <div class="container">
44
+          <a class="btn btn-navbar" data-target=".nav-collapse" data-toggle="collapse">
45
+            <span class="icon-bar"></span>
46
+            <span class="icon-bar"></span>
47
+            <span class="icon-bar"></span>
48
+          </a>
49
+          <%= render 'layouts/logo' %>
50
+
51
+          <%= render 'layouts/navigation_links' %>
52
+
53
+        </div>
54
+      </div>
55
+    </div>
56
+
57
+    <div class="container top-container ">
58
+      <%= bootstrap_flash %>
59
+      <%= yield %>
60
+
61
+      <%= render 'layouts/footer' %>
62
+
63
+
64
+    </div> <!-- /container -->
65
+
66
+  </body>
67
+</html>

+ 1 - 1
app/views/uploads/_form.html.erb

@@ -4,7 +4,7 @@
4 4
   <div class="form-inputs">
5 5
     <%= f.input :title %>
6 6
     <%= f.file_field :file %>
7
-    <%= f.input :desciption %>
7
+    <%= f.input :description %>
8 8
   </div>
9 9
 
10 10
   <div class="form-actions">

+ 9 - 2
app/views/uploads/show.html.erb

@@ -7,7 +7,7 @@
7 7
 
8 8
 <p>
9 9
   <strong>File:</strong><br>
10
-  <% if @upload.file.file.content_type == 'image/jpeg' || @upload.file.file.content_type == 'image/png' %>
10
+  <% if @upload.get_extension == 'jpg' || @upload.get_extension == 'png' %>
11 11
  	<%= image_tag @upload.file.to_s if @upload.file != nil %>
12 12
   <% else %>
13 13
   	<%= link_to @upload.title, @upload.file.to_s %>
@@ -15,9 +15,16 @@
15 15
 </p>
16 16
 
17 17
 <p>
18
+  <strong>File Type:</strong>
19
+  <%= @upload.file.file.content_type %>
20
+</p>
21
+
22
+<p>
18 23
   <strong>Desciption:</strong>
19
-  <%= @upload.desciption %>
24
+  <%= @upload.description %>
20 25
 </p>
21 26
 
27
+
22 28
 <%= link_to 'Edit', edit_upload_path(@upload) %> |
29
+<%= link_to 'Destroy', @upload, method: :delete, data: { confirm: 'Are you sure?' } %> |
23 30
 <%= link_to 'Back', admin_files_path %>

+ 1 - 1
config/routes.rb

@@ -7,7 +7,7 @@ RailsWebsiteTemplate::Application.routes.draw do
7 7
   get "admin/posts" => "admin_panel#posts", :as => :admin_posts
8 8
   get "admin/users" => "admin_panel#users", :as => :admin_users
9 9
   
10
-  post "upload" => "blog_posts#upload", :as => :post_upload
10
+  post "upload" => "uploads#upload", :as => :post_upload
11 11
   get "blog" => "blog_posts#index", :as => :blog
12 12
   get "post/:id" => "blog_posts#show", :as => :post
13 13
   resources :blog_posts, path: '/admin/posts'

+ 1 - 1
db/migrate/20140922074342_create_uploads.rb

@@ -3,7 +3,7 @@ class CreateUploads < ActiveRecord::Migration
3 3
     create_table :uploads do |t|
4 4
       t.string :title
5 5
       t.string :file
6
-      t.text :desciption
6
+      t.text :description
7 7
 
8 8
       t.timestamps
9 9
     end

+ 1 - 1
db/schema.rb

@@ -46,7 +46,7 @@ ActiveRecord::Schema.define(version: 20140922074342) do
46 46
   create_table "uploads", force: true do |t|
47 47
     t.string   "title"
48 48
     t.string   "file"
49
-    t.text     "desciption"
49
+    t.text     "description"
50 50
     t.datetime "created_at"
51 51
     t.datetime "updated_at"
52 52
   end