Setup blog controller and model.

This commit is contained in:
Andrew Tomaka 2012-05-04 23:55:58 -04:00
parent 87232d64ee
commit 08dff24760
12 changed files with 86 additions and 1 deletions

View file

@ -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/

View file

@ -0,0 +1,3 @@
// Place all the styles related to the Blogs controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

View file

@ -0,0 +1,19 @@
class BlogsController < ApplicationController
def index
@blogs = Blog.released
respond_to do |format|
format.html
format.json { render :json => @blogs }
end
end
def show
@blog = Blog.get(params[:id])
respond_to do |format|
format.html
format.json { render :json => @blog }
end
end

View file

@ -0,0 +1,2 @@
module BlogsHelper
end

9
app/models/blog.rb Normal file
View file

@ -0,0 +1,9 @@
class Blog < ActiveRecord::Base
attr_accessible :title, :body, :release
validates :title, :presence => true, :length { minimum: 3 }
validates :body, :presence => true
def self.released
Blog.where("DATE(release) <= DATE(?)", Time.now).order("release DESC")
end
end

View file

@ -1,5 +1,6 @@
WwwWhoisandrewCom::Application.routes.draw do WwwWhoisandrewCom::Application.routes.draw do
resources :links resources :links
resources :blogs
root :to => "home#index" root :to => "home#index"

View file

@ -0,0 +1,11 @@
class CreateBlogs < ActiveRecord::Migration
def change
create_table :blogs do |t|
t.string :title
t.text :body
t.datetime :release
t.timestamps
end
end
end

View file

@ -11,7 +11,15 @@
# #
# It's strongly recommended to check this file into your version control system. # It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20120503051402) do ActiveRecord::Schema.define(:version => 20120505032057) do
create_table "blogs", :force => true do |t|
t.string "title"
t.text "body"
t.datetime "release"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "links", :force => true do |t| create_table "links", :force => true do |t|
t.string "url" t.string "url"

11
test/fixtures/blogs.yml vendored Normal file
View file

@ -0,0 +1,11 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value

View file

@ -0,0 +1,7 @@
require 'test_helper'
class BlogsControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end

7
test/unit/blog_test.rb Normal file
View file

@ -0,0 +1,7 @@
require 'test_helper'
class BlogTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -0,0 +1,4 @@
require 'test_helper'
class BlogsHelperTest < ActionView::TestCase
end