Skip to content

Commit

Permalink
Add admin-only actions panel.
Browse files Browse the repository at this point in the history
  • Loading branch information
hmans committed Mar 3, 2012
1 parent 1bb874a commit 625561d
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Changes:

* The various available rake tasks have been moved to the `schnitzelpress` command line tool.
* Most of your blog's configuration is now stored in MongoDB and can be modified from the new "Configuration" page in your the admin panel.
* When logged in as an admin, you will be shown a small admin actions panel in the upper right corner of your browser, allowing you to quickly edit posts, jump to the admin section, or log out.
* Schnitzelpress now has a light-weight, custom-built asset pipeline that serves all Javascripts and Stylesheets as one single file each, compressed and ready for hardcore caching.
* Post with dates now use double-digit days and months in their canonical URLs. (Your existing posts will forward to the new canonical URLs automatically.)
* Various performance improvements.
Expand Down
47 changes: 47 additions & 0 deletions lib/assets/js/jquery.cookie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*!
* jQuery Cookie Plugin
* https://github.com/carhartl/jquery-cookie
*
* Copyright 2011, Klaus Hartl
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://www.opensource.org/licenses/mit-license.php
* http://www.opensource.org/licenses/GPL-2.0
*/
(function($) {
$.cookie = function(key, value, options) {

// key and at least value given, set cookie...
if (arguments.length > 1 && (!/Object/.test(Object.prototype.toString.call(value)) || value === null || value === undefined)) {
options = $.extend({}, options);

if (value === null || value === undefined) {
options.expires = -1;
}

if (typeof options.expires === 'number') {
var days = options.expires, t = options.expires = new Date();
t.setDate(t.getDate() + days);
}

value = String(value);

return (document.cookie = [
encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value),
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
options.path ? '; path=' + options.path : '',
options.domain ? '; domain=' + options.domain : '',
options.secure ? '; secure' : ''
].join(''));
}

// key and possibly options given, get cookie...
options = value || {};
var decode = options.raw ? function(s) { return s; } : decodeURIComponent;

var pairs = document.cookie.split('; ');
for (var i = 0, pair; pair = pairs[i] && pairs[i].split('='); i++) {
if (decode(pair[0]) === key) return decode(pair[1] || ''); // IE saves cookies with empty string as "c; ", e.g. without "=" as opposed to EOMB, thus pair[1] may be undefined
}
return null;
};
})(jQuery);
5 changes: 5 additions & 0 deletions lib/assets/js/schnitzelpress.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ $(document).ready(function() {
evt.preventDefault();
loginViaEmail();
});

var showAdmin = $.cookie('show_admin');
if (showAdmin) {
$('body').addClass('show_admin');
}
});
4 changes: 2 additions & 2 deletions lib/schnitzelpress/actions/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class JavascriptPacker
def self.pack_javascripts!(files)
plain = files.map do |filename|
File.read(File.expand_path("../lib/assets/js/#{filename}", settings.root))
end.join('\n')
end.join("\n")

Packr.pack(plain)
end
Expand All @@ -17,7 +17,7 @@ module Assets
extend ActiveSupport::Concern

ASSET_TIMESTAMP = Time.now.to_i
JAVASCRIPT_ASSETS = ['schnitzelpress.js']
JAVASCRIPT_ASSETS = ['jquery.cookie.js', 'schnitzelpress.js']

included do
get '/assets/schnitzelpress.:timestamp.css' do
Expand Down
9 changes: 8 additions & 1 deletion lib/schnitzelpress/actions/auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ module Auth
config.update_attributes!(:author_email => auth['uid'])
end

redirect admin_logged_in? ? '/admin/' : '/'
if admin_logged_in?
response.set_cookie('show_admin', :value => true, :path => '/')
redirect '/admin/'
else
redirect '/'
end
end

get '/login' do
Expand All @@ -27,6 +32,8 @@ module Auth

get '/logout' do
session[:auth] = nil
response.delete_cookie('show_admin')

redirect '/login'
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/schnitzelpress/app.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "sinatra/content_for"

module Schnitzelpress
class App < Sinatra::Base
STATIC_PATHS = ["/favicon.ico", "/img", "/js"]
Expand All @@ -13,6 +15,7 @@ class App < Sinatra::Base
use Rack::MethodOverride
use Rack::Session::Cookie

helpers Sinatra::ContentFor
helpers Schnitzelpress::Helpers
include Rack::Utils
include Schnitzelpress::Actions::Auth
Expand Down
5 changes: 5 additions & 0 deletions lib/views/layout.haml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
- if @show_description
~ markdown config.blog_description

#actions.admin_only
= yield_content :actions
%a{:href => '/admin/'} Go To Admin
%a{:href => '/logout'} Logout

= yield

%footer
Expand Down
3 changes: 3 additions & 0 deletions lib/views/post.haml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@
- if show_disqus? && @post.disqus?
%section.disqus
= partial 'disqus', :disqus_identifier => @post.disqus_identifier

- content_for :actions do
%a{:href => "/admin/edit/%s" % @post.id} Edit
31 changes: 29 additions & 2 deletions lib/views/schnitzelpress.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,34 @@ body, .container {
background-image: url(/img/background.png);
}

.admin_only {
display: none;
body.show_admin & { display: block; }
}

/* action panel */
#actions {
@include small-type;
margin: 5px;

a {
display: inline-block;
padding: 3px 6px;
background-color: $color-text;
color: $color-background;
font-weight: bold;
text-decoration: none;
border-radius: 3px;
@include appear-on-hover;
}

@media only screen and (min-width: 641px) {
position: fixed;
top: 0px;
right: 0px;
}
}

/* posts */
article.post {
header {
Expand All @@ -31,8 +59,7 @@ article.post {
@include animated;

margin: 2em 0;
opacity: 0.3;
&:hover { opacity: 1 }
@include appear-on-hover;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion schnitzelpress.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Gem::Specification.new do |gem|
gem.add_dependency 'rack-contrib', '~> 1.1.0'
gem.add_dependency 'rack-cache', '~> 1.1.0'
gem.add_dependency 'sinatra', '~> 1.3.2'
gem.add_dependency 'sinatra-contrib', '~> 1.3.1'
gem.add_dependency 'activesupport', '~> 3.2.0'

# database related
Expand All @@ -36,7 +37,7 @@ Gem::Specification.new do |gem|
gem.add_dependency 'sass', '~> 3.1.15'
gem.add_dependency 'redcarpet', '~> 2.1.0'
gem.add_dependency 'coderay', '~> 1.0.5'
gem.add_dependency 'schnitzelstyle', '~> 0.1.0'
gem.add_dependency 'schnitzelstyle', '~> 0.1.1'
gem.add_dependency 'i18n', '~> 0.6.0'
gem.add_dependency 'tilt', '~> 1.3.0'
gem.add_dependency 'ruby-oembed', '~> 0.8.5'
Expand Down

0 comments on commit 625561d

Please sign in to comment.