From 3b03e24dea86a18fb26e4ea35db9d3808729b91d Mon Sep 17 00:00:00 2001 From: chrjoh Date: Wed, 2 Nov 2011 18:47:26 +0100 Subject: [PATCH] added post controller and model --- Gemfile | 1 + Gemfile.lock | 3 ++ app/assets/javascripts/posts.js.coffee | 3 ++ app/assets/stylesheets/posts.css.scss | 3 ++ app/controllers/posts_controller.rb | 38 +++++++++++++++ app/helpers/posts_helper.rb | 2 + app/models/post.rb | 6 +++ app/views/posts/_form.html.haml | 24 ++++++++++ app/views/posts/create.html.haml | 2 + app/views/posts/destroy.html.haml | 2 + app/views/posts/edit.html.haml | 0 app/views/posts/index.html.haml | 20 ++++++++ app/views/posts/new.html.haml | 1 + app/views/posts/show.html.haml | 9 ++++ app/views/posts/update.html.haml | 2 + config/initializers/kaminari_config.rb | 8 ++++ config/routes.rb | 5 ++ spec/controllers/posts_controller_spec.rb | 54 ++++++++++++++++++++++ spec/factories/posts.rb | 9 ++++ spec/helpers/post_helper_spec.rb | 15 ++++++ spec/helpers/posts_helper_spec.rb | 15 ++++++ spec/models/post_spec.rb | 5 ++ spec/views/posts/create.html.haml_spec.rb | 5 ++ spec/views/posts/destroy.html.haml_spec.rb | 5 ++ spec/views/posts/index.html.haml_spec.rb | 5 ++ spec/views/posts/show.html.haml_spec.rb | 5 ++ spec/views/posts/update.html.haml_spec.rb | 5 ++ 27 files changed, 252 insertions(+) create mode 100644 app/assets/javascripts/posts.js.coffee create mode 100644 app/assets/stylesheets/posts.css.scss create mode 100644 app/controllers/posts_controller.rb create mode 100644 app/helpers/posts_helper.rb create mode 100644 app/models/post.rb create mode 100644 app/views/posts/_form.html.haml create mode 100644 app/views/posts/create.html.haml create mode 100644 app/views/posts/destroy.html.haml create mode 100644 app/views/posts/edit.html.haml create mode 100644 app/views/posts/index.html.haml create mode 100644 app/views/posts/new.html.haml create mode 100644 app/views/posts/show.html.haml create mode 100644 app/views/posts/update.html.haml create mode 100644 config/initializers/kaminari_config.rb create mode 100644 spec/controllers/posts_controller_spec.rb create mode 100644 spec/factories/posts.rb create mode 100644 spec/helpers/post_helper_spec.rb create mode 100644 spec/helpers/posts_helper_spec.rb create mode 100644 spec/models/post_spec.rb create mode 100644 spec/views/posts/create.html.haml_spec.rb create mode 100644 spec/views/posts/destroy.html.haml_spec.rb create mode 100644 spec/views/posts/index.html.haml_spec.rb create mode 100644 spec/views/posts/show.html.haml_spec.rb create mode 100644 spec/views/posts/update.html.haml_spec.rb diff --git a/Gemfile b/Gemfile index ab56021..09c3933 100644 --- a/Gemfile +++ b/Gemfile @@ -21,6 +21,7 @@ gem "mongoid" gem 'mongoid_taggable' gem 'mongoid-generator' gem "haml" +gem 'kaminari' gem "haml-rails", :group => :development # To use ActiveModel has_secure_password diff --git a/Gemfile.lock b/Gemfile.lock index 2aa37fe..e86adf6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -90,6 +90,8 @@ GEM thor (~> 0.14) json (1.6.1) json_pure (1.6.1) + kaminari (0.12.4) + rails (>= 3.0.0) launchy (2.0.5) addressable (~> 2.2.6) mail (2.3.0) @@ -205,6 +207,7 @@ DEPENDENCIES haml haml-rails jquery-rails + kaminari launchy mongoid mongoid-generator diff --git a/app/assets/javascripts/posts.js.coffee b/app/assets/javascripts/posts.js.coffee new file mode 100644 index 0000000..7615679 --- /dev/null +++ b/app/assets/javascripts/posts.js.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ diff --git a/app/assets/stylesheets/posts.css.scss b/app/assets/stylesheets/posts.css.scss new file mode 100644 index 0000000..1a7e153 --- /dev/null +++ b/app/assets/stylesheets/posts.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the posts controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb new file mode 100644 index 0000000..92cfba0 --- /dev/null +++ b/app/controllers/posts_controller.rb @@ -0,0 +1,38 @@ +class PostsController < ApplicationController + + def index + @posts = Post.all + @posts = @posts.page params[:page] unless @posts.count == 0 + respond_to do |format| + format.html + format.any(:json) { render request.format.to_sym => @posts } + end + end + + def show + @post = Post.find(params[:id]) + respond_to do |format| + format.html + format.any(:json) { render request.format.to_sym => @post } + end + end + + def create + @post = Post.new(params[:post]) + @post.save + redirect_to post_path(@post) + end + + def update + end + + def destroy + end + + def new + @post = Post.new + end + + def edit + end +end diff --git a/app/helpers/posts_helper.rb b/app/helpers/posts_helper.rb new file mode 100644 index 0000000..a7b8cec --- /dev/null +++ b/app/helpers/posts_helper.rb @@ -0,0 +1,2 @@ +module PostsHelper +end diff --git a/app/models/post.rb b/app/models/post.rb new file mode 100644 index 0000000..968c291 --- /dev/null +++ b/app/models/post.rb @@ -0,0 +1,6 @@ +class Post + include Mongoid::Document + field :title, :type => String + field :body, :type => String + field :published, :type => Boolean +end diff --git a/app/views/posts/_form.html.haml b/app/views/posts/_form.html.haml new file mode 100644 index 0000000..728f213 --- /dev/null +++ b/app/views/posts/_form.html.haml @@ -0,0 +1,24 @@ +.first.grid1   += form_for @post, :html => {:class => 'grid10 main_form'} do |f| + .first.grid10.box + .first.grid10.subhead + .first.grid2.label + %h3 Post + .first.grid2.label + = f.label :title, "Title" + .grid8.field + = f.text_field :title + + .first.grid2.label + = f.label :body, "Body" + .grid8.field + = f.text_area :body + + .first.grid2.label + = f.label :published, "Published" + .grid8.field + = f.check_box :published + + .first.grid10.submit + = f.submit :class => 'button save' + diff --git a/app/views/posts/create.html.haml b/app/views/posts/create.html.haml new file mode 100644 index 0000000..6bb4ce7 --- /dev/null +++ b/app/views/posts/create.html.haml @@ -0,0 +1,2 @@ +%h1 Posts#create +%p Find me in app/views/posts/create.html.haml \ No newline at end of file diff --git a/app/views/posts/destroy.html.haml b/app/views/posts/destroy.html.haml new file mode 100644 index 0000000..3354704 --- /dev/null +++ b/app/views/posts/destroy.html.haml @@ -0,0 +1,2 @@ +%h1 Posts#destroy +%p Find me in app/views/posts/destroy.html.haml \ No newline at end of file diff --git a/app/views/posts/edit.html.haml b/app/views/posts/edit.html.haml new file mode 100644 index 0000000..e69de29 diff --git a/app/views/posts/index.html.haml b/app/views/posts/index.html.haml new file mode 100644 index 0000000..8a0ba3f --- /dev/null +++ b/app/views/posts/index.html.haml @@ -0,0 +1,20 @@ +.grid1.first   + +.grid10.box + .first.grid10.subhead + .first.grid2.label + %h3 Posts + - for post in @posts + .first.grid10.article_row + .first.grid2 + = post.title + .grid6 + .first.grid6 + = post.body + + .first.grid10 + -#= paginate @posts + +.grid1.first   +.grid10 + = link_to('new post', new_post_url, :class => "button add") diff --git a/app/views/posts/new.html.haml b/app/views/posts/new.html.haml new file mode 100644 index 0000000..adfc4ab --- /dev/null +++ b/app/views/posts/new.html.haml @@ -0,0 +1 @@ += render :partial => 'form' \ No newline at end of file diff --git a/app/views/posts/show.html.haml b/app/views/posts/show.html.haml new file mode 100644 index 0000000..7c66710 --- /dev/null +++ b/app/views/posts/show.html.haml @@ -0,0 +1,9 @@ +.first.grid1   +.grid10.box + .first.grid10.subhead + .first.grid2.label + %h3 + = @post.title + .first.grid10.article_attribute + = simple_format(@post.body) + .first.grid10.article_attribute diff --git a/app/views/posts/update.html.haml b/app/views/posts/update.html.haml new file mode 100644 index 0000000..54aed5c --- /dev/null +++ b/app/views/posts/update.html.haml @@ -0,0 +1,2 @@ +%h1 Posts#update +%p Find me in app/views/posts/update.html.haml \ No newline at end of file diff --git a/config/initializers/kaminari_config.rb b/config/initializers/kaminari_config.rb new file mode 100644 index 0000000..0e5358c --- /dev/null +++ b/config/initializers/kaminari_config.rb @@ -0,0 +1,8 @@ +Kaminari.configure do |config| + # config.default_per_page = 25 + # config.window = 4 + # config.outer_window = 0 + # config.left = 0 + # config.right = 0 + # config.param_name = :page +end diff --git a/config/routes.rb b/config/routes.rb index f2d7213..eb0bdd8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,9 @@ Nssba::Application.routes.draw do + + resources :posts + + root :to => 'posts#index' + # The priority is based upon order of creation: # first created -> highest priority. diff --git a/spec/controllers/posts_controller_spec.rb b/spec/controllers/posts_controller_spec.rb new file mode 100644 index 0000000..2e4be98 --- /dev/null +++ b/spec/controllers/posts_controller_spec.rb @@ -0,0 +1,54 @@ +require 'spec_helper' + +describe PostsController do + + describe "GET 'index'" do + it "returns http success" do + get 'index' + response.should be_success + end + end + + describe "GET 'show'" do + it "returns http success" do + get 'show' + response.should be_success + end + end + + describe "GET 'create'" do + it "returns http success" do + get 'create' + response.should be_success + end + end + + describe "GET 'update'" do + it "returns http success" do + get 'update' + response.should be_success + end + end + + describe "GET 'destroy'" do + it "returns http success" do + get 'destroy' + response.should be_success + end + end + + describe "GET 'edit'" do + it "returns http success" do + get 'edit' + response.should be_success + end + end + + describe "GET 'new'" do + it "returns http success" do + get 'new' + response.should be_success + end + end + +end diff --git a/spec/factories/posts.rb b/spec/factories/posts.rb new file mode 100644 index 0000000..e2795c6 --- /dev/null +++ b/spec/factories/posts.rb @@ -0,0 +1,9 @@ +# Read about factories at http://github.com/thoughtbot/factory_girl + +FactoryGirl.define do + factory :post do + title "MyString" + body "MyText" + published false + end +end diff --git a/spec/helpers/post_helper_spec.rb b/spec/helpers/post_helper_spec.rb new file mode 100644 index 0000000..8746ec2 --- /dev/null +++ b/spec/helpers/post_helper_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +# Specs in this file have access to a helper object that includes +# the PostHelper. For example: +# +# describe PostHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# helper.concat_strings("this","that").should == "this that" +# end +# end +# end +describe PostHelper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/helpers/posts_helper_spec.rb b/spec/helpers/posts_helper_spec.rb new file mode 100644 index 0000000..19a1e23 --- /dev/null +++ b/spec/helpers/posts_helper_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +# Specs in this file have access to a helper object that includes +# the PostsHelper. For example: +# +# describe PostsHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# helper.concat_strings("this","that").should == "this that" +# end +# end +# end +describe PostsHelper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb new file mode 100644 index 0000000..98c3a24 --- /dev/null +++ b/spec/models/post_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe Post do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/views/posts/create.html.haml_spec.rb b/spec/views/posts/create.html.haml_spec.rb new file mode 100644 index 0000000..daad0bd --- /dev/null +++ b/spec/views/posts/create.html.haml_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe "posts/create.html.haml" do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/views/posts/destroy.html.haml_spec.rb b/spec/views/posts/destroy.html.haml_spec.rb new file mode 100644 index 0000000..8609afc --- /dev/null +++ b/spec/views/posts/destroy.html.haml_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe "posts/destroy.html.haml" do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/views/posts/index.html.haml_spec.rb b/spec/views/posts/index.html.haml_spec.rb new file mode 100644 index 0000000..209be84 --- /dev/null +++ b/spec/views/posts/index.html.haml_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe "posts/index.html.haml" do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/views/posts/show.html.haml_spec.rb b/spec/views/posts/show.html.haml_spec.rb new file mode 100644 index 0000000..1a38df6 --- /dev/null +++ b/spec/views/posts/show.html.haml_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe "posts/show.html.haml" do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/views/posts/update.html.haml_spec.rb b/spec/views/posts/update.html.haml_spec.rb new file mode 100644 index 0000000..98a902d --- /dev/null +++ b/spec/views/posts/update.html.haml_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe "posts/update.html.haml" do + pending "add some examples to (or delete) #{__FILE__}" +end