Setup basic Links model and controllers.

This commit is contained in:
Andrew Tomaka 2012-05-03 01:26:26 -04:00
parent 56f2b7348e
commit 5d0ac45115
23 changed files with 172 additions and 0 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 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 home 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,3 @@
// Place all the styles related to the Links 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,4 @@
class HomeController < ApplicationController
def index
end
end

View file

@ -0,0 +1,27 @@
class LinksController < ApplicationController
def index
@links = Link.released
respond_to do |format|
format.html
format.json { render :json => @links }
end
end
def new
end
def create
end
def show
@link = Link.goto(params[:id])
#update link count
respond_to do |format|
format.html { redirect_to @link.url }
format.json { render :json => @link }
end
end
end

View file

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

View file

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

21
app/models/link.rb Normal file
View file

@ -0,0 +1,21 @@
class Link < ActiveRecord::Base
attr_accessible :description, :release, :url
validates :url, :presence => true, :url => true
validates :description, :presence => true
def released
Link.where("DATE(release) <= DATE(?)", Time.now).order("release DESC")
end
def goto(id = nil)
return false if nil
Link.transaction do
link = Link.find(id)
link.visits += 1
link.save
end
return link
end
end

View file

@ -0,0 +1,2 @@
<h1>Home#index</h1>
<p>Find me in app/views/home/index.html.erb</p>

2
app/views/links/0 Normal file
View file

@ -0,0 +1,2 @@
 invoke active_record
 create db/migrate/20120503045327_change_column.rb

View file

@ -0,0 +1,2 @@
<h1>Links#index</h1>
<p>Find me in app/views/links/index.html.erb</p>

View file

@ -1,4 +1,8 @@
WwwWhoisandrewCom::Application.routes.draw do
resources :links
root :to => "home#index"
# The priority is based upon order of creation:
# first created -> highest priority.

View file

@ -0,0 +1,11 @@
class CreateLinks < ActiveRecord::Migration
def change
create_table :links do |t|
t.string :url
t.text :description
t.datetime :release
t.timestamps
end
end
end

View file

@ -0,0 +1,5 @@
class AddVisitsToLink < ActiveRecord::Migration
def change
add_column :links, :visits, :integer
end
end

View file

@ -0,0 +1,9 @@
class SetDefaultVisits < ActiveRecord::Migration
def up
change_column :links, :visits, :integer, :null => false, :default => 0
end
def down
raise ActiveRecord::IrreversibleMigration, "Can't remove the default"
end
end

25
db/schema.rb Normal file
View file

@ -0,0 +1,25 @@
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20120503051402) do
create_table "links", :force => true do |t|
t.string "url"
t.text "description"
t.datetime "release"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "visits", :default => 0, :null => false
end
end

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

@ -0,0 +1,11 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
one:
url: MyString
description: MyText
release: 2012-05-02 23:03:09
two:
url: MyString
description: MyText
release: 2012-05-02 23:03:09

View file

@ -0,0 +1,9 @@
require 'test_helper'
class HomeControllerTest < ActionController::TestCase
test "should get index" do
get :index
assert_response :success
end
end

View file

@ -0,0 +1,9 @@
require 'test_helper'
class LinksControllerTest < ActionController::TestCase
test "should get index" do
get :index
assert_response :success
end
end

View file

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

View file

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

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

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