Permalink
Browse files

added post administration

  • Loading branch information...
1 parent a39467b commit b16e4af7a57986d46c81a3f9961dfb5080e7e99d @rapind committed Mar 30, 2012
View
4 app/assets/stylesheets/application.css.sass
@@ -107,7 +107,9 @@ abbr[title='required']
width: 100px
select[name*='(3i)'], select[name*='(4i)'], select[name*='(5i)']
width: 60px
- input[name*='[name]'], input[name*='[keywords]'], input[name*='url]']
+ input[name*='[name]']
+ width: 400px
+ input[name*='[title]'], input[name*='[keywords]'], input[name*='url]']
width: 690px
input.boolean
margin: 7px 10px 0 0
View
2 app/controllers/admin/home_controller.rb
@@ -3,7 +3,7 @@ class Admin::HomeController < ApplicationController
before_filter :authenticate_photographer!
def index
- @events = Event.limit()
+ @events = Event.limit(10)
end
end
View
30 app/controllers/admin/posts_controller.rb
@@ -0,0 +1,30 @@
+class Admin::PostsController < Admin::HomeController
+ inherit_resources
+ actions :all, :except => :show
+
+ def index
+ params[:order] = 'title' unless params[:order]
+ @posts = Post.order(params[:order])
+
+ if defined?(params[:search][:q]) and params[:search][:q]
+ term = Regexp.escape(params[:search][:q])
+ @posts = @posts.where(["title ~* ? OR keywords ~* ?", term, term])
+ end
+
+ respond_to do |format|
+ format.html { @posts = @posts.page(params[:page]) } # page html view
+ format.json { @posts = @posts.limit(25) } # limit json view
+ end
+ end
+
+ # Redirect to the collection path on create.
+ def create
+ create!{ collection_path }
+ end
+
+ # Redirect to the collection path on update.
+ def update
+ update!{ collection_path }
+ end
+
+end
View
7 app/controllers/posts_controller.rb
@@ -2,4 +2,11 @@ class PostsController < ApplicationController
inherit_resources
respond_to :html
actions :index, :show
+
+ private #----
+
+ def collection
+ @posts ||= end_of_association_chain.page(params[:page]).per(5)
+ end
+
end
View
11 app/helpers/application_helper.rb
@@ -1,2 +1,13 @@
module ApplicationHelper
+
+ def keywords_to_labels(keywords)
+ result = ''
+ unless keywords.blank?
+ keywords.split(', ').each do |keyword|
+ result += content_tag(:span, keyword, :class => "label label-info") + ' '
+ end
+ end
+ result.html_safe
+ end
+
end
View
15 app/models/event.rb
@@ -16,4 +16,19 @@ class Event < ActiveRecord::Base
# Default page size
paginates_per 15
+ # ****
+ # Callbacks
+ after_save :prune
+
+ private #----
+
+ # ****
+ # Only retain the last 60 events
+ def prune
+ if Event.count > 60
+ cutoff = Event.limit(60).last.created_at
+ Event.where(["created_at < ?", cutoff]).delete_all
+ end
+ end
+
end
View
8 app/models/page.rb
@@ -32,17 +32,17 @@ def to_param
after_save :clear_cache
def self.cached
- Rails.cache.fetch(CACHED, :expires_in => 1.day) do
+ #Rails.cache.fetch(CACHED, :expires_in => 1.day) do
self.order(:name).all
- end
+ #end
end
def clear_cache
- Page.clear_cache
+ #Rails.cache.delete(CACHED)
end
def self.clear_cache
- Rails.cache.delete(CACHED)
+ #Rails.cache.delete(CACHED)
end
# ****
View
8 app/models/photographer.rb
@@ -32,17 +32,17 @@ class Photographer < ActiveRecord::Base
after_save :clear_cache
def self.cached
- Rails.cache.fetch(CACHED, :expires_in => 1.day) do
+ #Rails.cache.fetch(CACHED, :expires_in => 1.day) do
self.first
- end
+ #end
end
def clear_cache
- Photographer.clear_cache
+ #Rails.cache.delete(CACHED)
end
def self.clear_cache
- Rails.cache.delete(CACHED)
+ #Rails.cache.delete(CACHED)
end
# ****
View
8 app/models/post.rb
@@ -32,17 +32,17 @@ def to_param
after_save :clear_cache
def self.recent
- Rails.cache.fetch(CACHED, :expires_in => 1.day) do
+ #Rails.cache.fetch(CACHED, :expires_in => 1.day) do
self.order(:created_at).limit(3).all
- end
+ #end
end
def clear_cache
- Post.clear_cache
+ #Rails.cache.delete(CACHED)
end
def self.clear_cache
- Rails.cache.delete(CACHED)
+ #Rails.cache.delete(CACHED)
end
# ****
View
18 app/views/admin/posts/_form.html.haml
@@ -0,0 +1,18 @@
+- content_for :title, "#{controller.action_name.titleize} Post"
+- content_for :sidebar do
+ %p Enter a descriptive title for the post. I.e. Newborn Sessions October 2012.
+ %p Optionally upload an image that will accompany your body content.
+ %p It's advisable that you fill out keywords with relevant content in order to list higher for related terms in search engines.
+ %p The body is the main content of the post and you can use markdown to add some HTML flair.
+
+= render "shared/error_messages", :target => @post
+
+= form.input :title, :hint => 'Give your post a meaningful title. Like "Newborn Sessions October 2012"'
+= form.input :keywords, :hint => 'Enter some keywords that pertain to this post. I.e. newborns, family, portrait, lifestyle, etc.'
+= form.input :image, :as => :file, :hint => 'You may upload an image to accompany the post.'
+= form.hidden_field :retained_image
+= link_to( image_tag(@post.image.thumb('100x100').url), @post.image.url, :rel => 'lightbox' ) if @post.image_uid
+= form.input :body, :hint => 'The contents of the post. You may use markdown.'
+
+.form-actions
+ = form.button :submit, :class => 'btn btn-primary'
View
2 app/views/admin/posts/edit.html.haml
@@ -0,0 +1,2 @@
+= simple_form_for(@post, :url => resource_path, :method => :put, :html => { :class => 'form-vertical' }) do |form|
+ = render form
View
22 app/views/admin/posts/index.html.haml
@@ -0,0 +1,22 @@
+- content_for :title do
+ Posts
+ .pull-right
+ = form_for :search, :url => collection_path, :method => :get, :class => 'form-search' do |form|
+ = form.text_field :q, :placeholder => 'Search', :class => 'input-medium search-query', :value => defined?(params[:search][:q]) ? params[:search][:q] : ''
+
+%p= link_to(raw("<i class='icon-plus icon-white'></i> New Post"), new_resource_path, :class => 'btn btn-success')
+
+%table.table.table-bordered.table-striped
+ %thead
+ %tr
+ %th Title
+ %th Created on
+ %th{ :colspan => 3, :width => '15%' } Actions
+ %tbody
+ - @posts.each do |obj|
+ %tr
+ %td= obj.title
+ %td= obj.created_at.strftime("%b %d, %Y")
+ %td= link_to('View', post_path(obj))
+ %td= link_to('Edit', edit_resource_path(obj))
+ %td= link_to('Delete', resource_path(obj), :method => :delete, :confirm => 'Are you sure?', :remote => true, 'data-type' => 'json', :class => 'delete')
View
2 app/views/admin/posts/new.html.haml
@@ -0,0 +1,2 @@
+= simple_form_for(@post, :url => collection_path, :html => { :class => 'form-vertical' }) do |form|
+ = render form
View
1 app/views/layouts/admin.html.haml
@@ -15,6 +15,7 @@
%li{ :class => controller.controller_name == 'private_galleries' ? 'active' : '' }= link_to 'Private Galleries', admin_private_galleries_path
%li{ :class => controller.controller_name == 'galleries' ? 'active' : '' }= link_to 'Galleries', admin_galleries_path
%li{ :class => controller.controller_name == 'pages' ? 'active' : '' }= link_to 'Pages', admin_pages_path
+ %li{ :class => controller.controller_name == 'posts' ? 'active' : '' }= link_to 'Posts', admin_posts_path
%li{ :class => controller.controller_name == 'photographers' ? 'active' : '' }= link_to 'Settings', edit_admin_photographer_path
%li.divider-vertical
%li= link_to 'Logout', destroy_photographer_session_path, :method => :delete
View
2 app/views/layouts/application.html.haml
@@ -14,7 +14,7 @@
%li{ :class => (defined?(@gallery) and @gallery.id == gallery.id ? 'active' : '') }= link_to(gallery.name, gallery_path(gallery))
- pages.each do |page|
%li{ :class => (defined?(@page) and @page.id == page.id ? 'active' : '') }= link_to(page.name, page_path(page))
- %li= link_to('Blog', posts_path)
+ %li{ :class => controller.controller_name == 'posts' ? 'active' : '' }= link_to('Blog', posts_path)
%li.divider-vertical
%li{ :class => controller.controller_name == 'contact_requests' ? 'active' : '' }= link_to('Contact', new_contact_request_path)
.pull-right= link_to(photographer.name, root_path, :class => 'brand')
View
11 app/views/posts/index.html.haml
@@ -0,0 +1,11 @@
+- content_for :title, 'Blog'
+
+%ul.unstyled
+ - @posts.each do |post|
+ %li
+ %h2= link_to(post.title, post_path(post))
+ %p= keywords_to_labels(post.keywords)
+ = truncate(post.body, :length => 500).to_html.html_safe
+
+%br
+%p= paginate @posts
View
2 app/views/posts/show.html.haml
@@ -2,7 +2,7 @@
- unless @post.keywords.blank?
- content_for :subtitle do
- @post.keywords.split(', ').each do |keyword|
- %span.label= keyword
+ %span.label.label-info= keyword
= image_tag(@post.image.url, :class => 'post-image') if @post.image_uid
= @post.body.to_html.html_safe
View
1 config/routes.rb
@@ -18,6 +18,7 @@
resource :events, :only => :index
resource :photographer, :only => [:edit, :update]
resources :pages
+ resources :posts
resources :galleries do
resources :photos
end
View
2 db/seeds.rb
@@ -56,6 +56,6 @@
Page.create(:name => "Pricing", :image => File.new(Rails.root.join('creatives', 'pages', 'about.png')), :body => "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n\nDuis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")
# Creating posts
-Post.create(:title => "Baby Stuey at 1 week", :keywords => 'baby, photography, newborns', :image => File.new(Rails.root.join('creatives', 'pages', 'about.png')), :body => "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n\nDuis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")
+Post.create(:title => "Baby Stuey at One Eeek", :keywords => 'baby, photography, newborns', :image => File.new(Rails.root.join('creatives', 'pages', 'about.png')), :body => "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n\nDuis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")
Post.create(:title => "The Jones Family Portraits", :keywords => 'family, photography, portrait, lifestyle', :image => File.new(Rails.root.join('creatives', 'pages', 'about.png')), :body => "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n\nDuis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")
Post.create(:title => "This is how we handle titles that are extraordinarily long.", :image => File.new(Rails.root.join('creatives', 'pages', 'about.png')), :body => "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n\nDuis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")

0 comments on commit b16e4af

Please sign in to comment.