Permalink
Browse files

rack-flash added + gallery view

  • Loading branch information...
zlorfi committed Mar 18, 2013
1 parent d56a374 commit add4ef15f85f39365778180c5152cb8acb8092d1
View
@@ -4,3 +4,4 @@ body
meta
.DS_Store
upload/*
+public/thumbnails
View
@@ -8,6 +8,7 @@ gem 'haml'
gem 'rack-cache', :require => 'rack/cache'
gem 'dragonfly'
gem 'mongoid'
+gem 'rack-flash3'
group :development do
gem 'shotgun'
View
@@ -39,6 +39,8 @@ GEM
rack (1.5.2)
rack-cache (1.2)
rack (>= 0.4)
+ rack-flash3 (1.0.3)
+ rack
rack-protection (1.5.0)
rack
sass (3.2.7)
@@ -65,6 +67,7 @@ DEPENDENCIES
pry
puma
rack-cache
+ rack-flash3
shotgun
sinatra (~> 1.3)
zurb-foundation
View
29 app.rb
@@ -6,8 +6,12 @@
require 'haml'
require 'dragonfly'
require 'mongoid'
+require 'rack-flash'
+require './helper/dragonfly_helper'
class App < Sinatra::Base
+ helpers Sinatra::DragonflyHelper
+
configure :production, :development do
enable :logging
end
@@ -40,16 +44,7 @@ class App < Sinatra::Base
set :scss, Compass.sass_engine_options
set :server, :puma
enable :sessions
- end
-
- helpers do
- def flash(message = '')
- session[:flash] = message
- end
- end
-
- before do
- @flash = session.delete(:flash)
+ use Rack::Flash, :sweep => true
end
class Picture
@@ -79,15 +74,21 @@ class Picture
image_uid = app.store(file, :meta => {:time => Time.now, :name => filename})
picture = Picture.create(image_uid: image_uid, image_name: filename)
- flash "Upload successful of #{filename}"
+ flash[:success] = "Upload successful of #{filename}"
+ redirect "/gallery/#{picture.id}"
else
- flash 'You have to choose a file'
+ flash[:alert] = 'You have to choose a file first'
+ redirect "/upload"
end
- redirect '/upload'
end
- get '/:image_id' do |image_id|
+ get "/gallery" do
+ @pictures = Picture.all
+ haml :gallery
+ end
+
+ get '/gallery/:image_id' do |image_id|
@image = Picture.find(image_id).image
haml :show
end
View
@@ -0,0 +1,49 @@
+require 'sinatra'
+module Sinatra
+ module DragonflyHelper
+
+ # Cache Dragonfly thumbnail generation and produce friendly SEO URLs
+ def thumbnail_tag(image, size, options={})
+
+ if options.has_key?(:path)
+ sub_dir = options[:path]
+ options.delete(:path)
+ else
+ sub_dir = 'thumbnails'
+ end
+
+ path = File.join('/', sub_dir, size, image.name)
+ resource = File.join('public', path)
+
+ unless File.exists?(resource)
+ image.thumb(size).to_file(resource)
+ end
+
+ attrs = ''
+ options.each do |key, value|
+ if key == :data
+ value.each do |key, value|
+ attrs << "data-#{key.to_s}='#{value.to_s}' "
+ end
+ else
+ attrs << "#{key.to_s}='#{value.to_s}' "
+ end
+ end
+
+ attrs << "alt='#{File.basename(image.name, '.*')}'" if options[:alt].blank?
+
+ # Optionnal stuff : since resize params can be very tricky (ex: '400x300+50+100'),
+ # make sure they are in simple 'widthxheight' format to pick up width/height params
+ # If not, just forget it and leave the browser deal with image dimensions
+ width, height = ''
+ if(size.split('x')[0].to_i.to_s == size.split('x')[0] && size.split('x')[1].to_i.to_s == size.split('x')[1])
+ width = "width='#{size.split('x')[0].to_i}px'"
+ height = "height='#{size.split('x')[1].to_i}px'"
+ end
+
+ "<img src='#{path}' #{width} #{height} #{attrs} />".html_safe
+ end
+
+ end
+ helpers DragonflyHelper
+end
@@ -0,0 +1,3 @@
+$(document).ready(function() {
+ $('.alert-box.success').delay(5000).fadeOut(500);
+});

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -0,0 +1,10 @@
+.row
+ .large-8.columns
+ = @pictures.count
+ %br/
+ %ul{"data-options" => "timer_speed:2500;", "data-orbit" => ""}
+ - @pictures.each do |key|
+ %li
+ -#%img{:src => "#{key.image.thumb('600x600').url}"}/
+ = thumbnail_tag key.image, '500x500'
+ .orbit-caption "#{key.image.width}"
View
@@ -15,36 +15,39 @@
%link{:href => "/stylesheets/normalize.css", :rel => "stylesheet"}
%link{:href => "/stylesheets/app.css", :rel => "stylesheet"}
%script{:src => "/javascripts/vendor/custom.modernizr.js"}
- %script{:src => "/javascripts/vendor/jquery.js"}
+ %script{:src => "/javascripts/vendor/jquery.min.js"}
+ %script{:src => "/javascripts/app.js"}
+ /:javascript
+ / document.write('<script src=' +
+ / ('__proto__' in {} ? 'javascripts/vendor/zepto' : 'javascripts/vendor/jquery') +
+ / '.js><\/script>')
/ IE Fix for HTML5 Tags
/[if lt IE 9]
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
%body
.row
.large-12.columns
- %h2 Gallery
- -if @flash
- .alert-box.success{"data-alert" => ""}
- = @flash
- %a.close{:href => "#"} ×
- %hr/
+ = haml :top_nav
+ = haml :notification
+ -# %hr/
=yield
+ %script{:src => "/javascripts/foundation.min.js"}
/ Included JS Files (Uncompressed)
- %script{:src => "/javascripts/foundation/foundation.js"}
- %script{:src => "/javascripts/foundation/foundation.alerts.js"}
- %script{:src => "/javascripts/foundation/foundation.clearing.js"}
- %script{:src => "/javascripts/foundation/foundation.cookie.js"}
- %script{:src => "/javascripts/foundation/foundation.dropdown.js"}
- %script{:src => "/javascripts/foundation/foundation.forms.js"}
- %script{:src => "/javascripts/foundation/foundation.joyride.js"}
- %script{:src => "/javascripts/foundation/foundation.magellan.js"}
- %script{:src => "/javascripts/foundation/foundation.orbit.js"}
- %script{:src => "/javascripts/foundation/foundation.placeholder.js"}
- %script{:src => "/javascripts/foundation/foundation.reveal.js"}
- %script{:src => "/javascripts/foundation/foundation.section.js"}
- %script{:src => "/javascripts/foundation/foundation.tooltips.js"}
- %script{:src => "/javascripts/foundation/foundation.topbar.js"}
+ /%script{:src => "/javascripts/foundation/foundation.js"}
+ /%script{:src => "/javascripts/foundation/foundation.alerts.js"}
+ /%script{:src => "/javascripts/foundation/foundation.clearing.js"}
+ /%script{:src => "/javascripts/foundation/foundation.cookie.js"}
+ /%script{:src => "/javascripts/foundation/foundation.dropdown.js"}
+ /%script{:src => "/javascripts/foundation/foundation.forms.js"}
+ /%script{:src => "/javascripts/foundation/foundation.joyride.js"}
+ /%script{:src => "/javascripts/foundation/foundation.magellan.js"}
+ /%script{:src => "/javascripts/foundation/foundation.orbit.js"}
+ /%script{:src => "/javascripts/foundation/foundation.placeholder.js"}
+ /%script{:src => "/javascripts/foundation/foundation.reveal.js"}
+ /%script{:src => "/javascripts/foundation/foundation.section.js"}
+ /%script{:src => "/javascripts/foundation/foundation.tooltips.js"}
+ /%script{:src => "/javascripts/foundation/foundation.topbar.js"}
:javascript
$(document).foundation();
View
@@ -0,0 +1,5 @@
+- %w[alert success].each do |key|
+ - if flash[key]
+ %div{:class => "alert-box #{key}", "data-alert" => ""}
+ = flash[key]
+ %a.close{:href => "#"} &times;
@@ -953,10 +953,10 @@
//
// Background color for the top bar
-// $topbar-bg: #111;
+$topbar-bg: #444;
// Height and margin
-// $topbar-height: 45px;
+$topbar-height: 40px;
// $topbar-margin-bottom: emCalc(30px);
// Control Input height for top bar
View
@@ -0,0 +1,28 @@
+%nav.top-bar
+ %ul.title-area
+ / Title Area
+ %li.name
+ %h1
+ %a{:href => "/"} Sinatra Gallery
+ / Remove the class "menu-icon" to get rid of menu icon. Take out "Menu" to just have icon alone
+ %li.toggle-topbar.menu-icon
+ %a{:href => "/"}
+ %span Menu
+ %section.top-bar-section
+ / Left Nav Section
+ %ul.left
+ %li.divider
+ / Right Nav Section
+ %ul.right
+ %li.divider.hide-for-small
+ %li
+ %a{:href => "/upload"} Upload
+ %li.divider
+ %li.has-form
+ %form
+ .row.collapse
+ .small-8.columns
+ %input{:type => "text"}/
+ .small-4.columns
+ %a.alert.button{:href => "#"} Search
+ %li.divider.show-for-small
View
@@ -9,7 +9,7 @@
%label.fileinput-button
%span Upload Document
%input{:type => :file, :name => :file}
- %input{:type => "submit",:value => "Upload"}
+ %input.small.button{:type => "submit",:value => "Upload"}
%script{:type => "text/javascript", :src => "/javascripts/vendor/jquery.ui.widget.js"}

0 comments on commit add4ef1

Please sign in to comment.