hitchens 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ module Hitchens
2
+ module Admin
3
+ class BaseController < ApplicationController
4
+ before_filter :authenticate_hitchens_admin
5
+ skip_authorization_check :only => [:index]
6
+
7
+ def index
8
+ end
9
+
10
+ private
11
+
12
+ def authenticate_hitchens_admin
13
+ unless current_hitchens_user && current_hitchens_user.blog_admin?
14
+ flash.alert = t("hitchens.errors.access_denied")
15
+ redirect_to main_app.__send__(Hitchens.sign_in_url_helper)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,45 @@
1
+ module Hitchens
2
+ module Admin
3
+ class PostsController < BaseController
4
+ load_and_authorize_resource :class => 'Hitchens::Post'
5
+
6
+ def index
7
+ @posts = @posts.page(params[:page]).per_page(Hitchens.posts_per_page)
8
+ @posts = PostDecorator.decorate @posts
9
+ end
10
+
11
+ def show
12
+ @post = PostDecorator.decorate @post
13
+ end
14
+
15
+ def create
16
+ if @post.save
17
+ flash[:notice] = t 'hitchens.notices.post_created'
18
+ redirect_to admin_posts_path
19
+ else
20
+ render 'new'
21
+ end
22
+ end
23
+ def update
24
+ if @post.update_attributes params[:post]
25
+ flash[:notice] = t 'hitchens.notices.post_updated'
26
+ redirect_to admin_posts_path
27
+ else
28
+ flash[:error] = t 'hitchens.errors.post_not_updated'
29
+ render 'edit'
30
+ end
31
+ end
32
+
33
+ def new
34
+ end
35
+ def edit
36
+ end
37
+ def destroy
38
+ @post.destroy
39
+ flash[:notice] = t('hitchens.notices.post_deleted')
40
+ redirect_to admin_posts_path
41
+ end
42
+ end
43
+ end
44
+ end
45
+
@@ -1,16 +1,15 @@
1
1
  module Hitchens
2
2
  class ApplicationController < ActionController::Base
3
3
  check_authorization
4
- load_and_authorize_resource
5
4
  layout Hitchens.use_parent_layout ? 'application' : 'hitchens/application'
6
5
 
7
6
  def current_ability
8
7
  # we need to tell CanCan to use our Hitchens::Ability class
9
- @current_ability ||= Ability.new(get_current_user)
8
+ @current_ability ||= Ability.new(current_hitchens_user)
10
9
  end
11
10
 
12
11
  private
13
- def get_current_user
12
+ def current_hitchens_user
14
13
  __send__ Hitchens.current_user_helper_name
15
14
  end
16
15
  end
@@ -1,5 +1,7 @@
1
1
  module Hitchens
2
2
  class PostsController < ApplicationController
3
+ load_and_authorize_resource
4
+
3
5
  def index
4
6
  @posts = @posts.page(params[:page]).per_page(Hitchens.posts_per_page)
5
7
  @posts = PostDecorator.decorate @posts
@@ -8,25 +10,5 @@ module Hitchens
8
10
  def show
9
11
  @post = PostDecorator.decorate @post
10
12
  end
11
-
12
- def create
13
- if @post.save
14
- redirect_to posts_path
15
- else
16
- render 'new'
17
- end
18
- end
19
- def update
20
- if @post.update_attributes params[:post]
21
- redirect_to posts_path
22
- else
23
- render 'edit'
24
- end
25
- end
26
-
27
- def new
28
- end
29
- def edit
30
- end
31
13
  end
32
14
  end
@@ -1,7 +1,7 @@
1
1
  module Hitchens
2
2
  class ApplicationDecorator < Draper::Base
3
3
  def markdown(text)
4
- syntax_highlighter(markdown_parser.render(text)).html_safe
4
+ markdown_parser.render(text).html_safe
5
5
  end
6
6
 
7
7
  private
@@ -10,16 +10,5 @@ private
10
10
  :autolink => true, :space_after_headers => true,
11
11
  :fenced_code_blocks => true)
12
12
  end
13
-
14
- def syntax_highlighter(html)
15
- doc = Nokogiri::HTML(html)
16
- doc.search("code").each do |pre|
17
- code_ray = CodeRay.scan(pre.text.rstrip, pre[:class]).div
18
- # first line of code wasn't indented as much as others
19
- # need to add a line break after the pre tag
20
- pre.replace code_ray.sub('<pre>', "<pre>\n")
21
- end
22
- doc.to_s
23
- end
24
13
  end
25
14
  end
@@ -3,7 +3,8 @@ module Hitchens
3
3
  decorates 'Hitchens::Post'
4
4
 
5
5
  def body_to_html
6
- markdown(body)
6
+ html = markdown(body)
7
+ syntax_highlight(html).html_safe
7
8
  end
8
9
 
9
10
  def pubdate_tag
@@ -18,5 +19,16 @@ module Hitchens
18
19
  def pubdate_display
19
20
  publication_date.getlocal.strftime("%A, %B %e, %Y at %l:%M %p")
20
21
  end
22
+ def syntax_highlight(html)
23
+ doc = Nokogiri::HTML.fragment(html)
24
+ # remove all pre tags.
25
+ # coderay will nest pre tags within the code tags.
26
+ doc.css("pre").each { |pre| pre.replace pre.inner_html }
27
+ doc.css("code").each do |code|
28
+ code_ray = CodeRay.scan(code.text.rstrip, code[:class]).div
29
+ code.replace code_ray
30
+ end
31
+ doc.to_s
32
+ end
21
33
  end
22
34
  end
@@ -0,0 +1,3 @@
1
+ .sign_out_link= link_to 'sign out', main_app.__send__(Hitchens.sign_out_url_helper), method: 'delete'
2
+ = link_to 'new post', new_admin_post_path if can? :create, @post
3
+ = link_to 'manage posts', admin_posts_path
@@ -1,4 +1,4 @@
1
- = simple_form_for post do |f|
1
+ = simple_form_for [:admin, post] do |f|
2
2
  = f.input :title
3
3
  = f.input :body
4
4
  = f.input :published, as: :boolean
@@ -0,0 +1,7 @@
1
+ = content_tag_for :div, post do
2
+ %h2.title= post.title
3
+ %p.dateline= post.pubdate_tag
4
+ -# %p.body= find_and_preserve post.body_to_html
5
+ = link_to 'Edit', edit_admin_post_path(post) if can? :update, post
6
+ = link_to 'Delete', admin_post_path(post), :method => :delete if can? :delete, post
7
+
@@ -0,0 +1,7 @@
1
+ = link_to 'new post', new_admin_post_path if can? :create, @post
2
+ = render partial: 'post', collection: @posts
3
+ = will_paginate @posts,
4
+ previous_label: 'newer posts',
5
+ next_label: 'older posts',
6
+ page_links: false
7
+
@@ -1,5 +1,5 @@
1
1
  = content_tag_for :div, post do
2
- %h2.title= post.title
2
+ %h2.title
3
+ = link_to post.title, post
3
4
  %p.dateline= post.pubdate_tag
4
- %p.body= post.body_to_html
5
- = link_to 'Edit', edit_post_path(post) if can? :update, post
5
+ %p.body= find_and_preserve post.body_to_html
@@ -1,4 +1,3 @@
1
- = link_to 'new post', new_post_path if can? :create, @post
2
1
  = render partial: 'post', collection: @posts
3
2
  = will_paginate @posts,
4
3
  previous_label: 'newer posts',
data/config/routes.rb CHANGED
@@ -1,4 +1,9 @@
1
1
  Hitchens::Engine.routes.draw do
2
2
  resources :posts
3
3
  root :to => 'posts#index'
4
+
5
+ namespace :admin do
6
+ root :to => "base#index"
7
+ resources :posts
8
+ end
4
9
  end
@@ -1,3 +1,3 @@
1
1
  module Hitchens
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/hitchens.rb CHANGED
@@ -17,7 +17,9 @@ module Hitchens
17
17
  :use_parent_layout,
18
18
  :user_class_name,
19
19
  :current_user_helper_name,
20
- :blog_admin_user_method
20
+ :blog_admin_user_method,
21
+ :sign_in_url_helper,
22
+ :sign_out_url_helper
21
23
 
22
24
  #TODO: set these up in the Hitchens initializer file
23
25
  self.blog_name = "Ian's test blog"
@@ -27,6 +29,8 @@ module Hitchens
27
29
  self.user_class_name = 'User'
28
30
  self.current_user_helper_name = 'current_user'
29
31
  self.blog_admin_user_method = 'blog_admin?'
32
+ self.sign_in_url_helper = 'new_user_session_path'
33
+ self.sign_out_url_helper = 'destroy_user_session_path'
30
34
 
31
35
  def self.user_class
32
36
  user_class_name.constantize
@@ -6,4 +6,9 @@ class User < ActiveRecord::Base
6
6
 
7
7
  # Setup accessible (or protected) attributes for your model
8
8
  attr_accessible :email, :password, :password_confirmation, :remember_me
9
+
10
+ # let's assume they're an admin if they have a real account
11
+ def blog_admin?
12
+ email.present?
13
+ end
9
14
  end
@@ -7,6 +7,12 @@
7
7
  <%= csrf_meta_tags %>
8
8
  </head>
9
9
  <body>
10
+ <% if flash[:notice] %>
11
+ <p class="notice"><%= flash[:notice] %></p>
12
+ <% end %>
13
+ <% if flash[:error] %>
14
+ <p class="error"><%= flash[:error] %></p>
15
+ <% end %>
10
16
  <p>
11
17
  <% if current_user %>
12
18
  <%= link_to 'sign out', main_app.destroy_user_session_path, method: 'delete' %>
@@ -2,4 +2,12 @@
2
2
  # See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3
3
 
4
4
  en:
5
- hello: "Hello world"
5
+ hitchens:
6
+ errors:
7
+ access_denied: Access denied.
8
+ post_not_updated: Post could not be updated.
9
+ notices:
10
+ post_deleted: Post successfully deleted.
11
+ post_created: Post successfully created.
12
+ post_updated: Post successfully updated.
13
+