Setup basic Links model and controllers.
This commit is contained in:
parent
56f2b7348e
commit
5d0ac45115
23 changed files with 172 additions and 0 deletions
3
app/assets/javascripts/home.js.coffee
Normal file
3
app/assets/javascripts/home.js.coffee
Normal 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/
|
3
app/assets/javascripts/links.js.coffee
Normal file
3
app/assets/javascripts/links.js.coffee
Normal 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/
|
3
app/assets/stylesheets/home.css.scss
Normal file
3
app/assets/stylesheets/home.css.scss
Normal 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/
|
3
app/assets/stylesheets/links.css.scss
Normal file
3
app/assets/stylesheets/links.css.scss
Normal 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/
|
4
app/controllers/home_controller.rb
Normal file
4
app/controllers/home_controller.rb
Normal file
|
@ -0,0 +1,4 @@
|
|||
class HomeController < ApplicationController
|
||||
def index
|
||||
end
|
||||
end
|
27
app/controllers/links_controller.rb
Normal file
27
app/controllers/links_controller.rb
Normal 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
|
2
app/helpers/home_helper.rb
Normal file
2
app/helpers/home_helper.rb
Normal file
|
@ -0,0 +1,2 @@
|
|||
module HomeHelper
|
||||
end
|
2
app/helpers/links_helper.rb
Normal file
2
app/helpers/links_helper.rb
Normal file
|
@ -0,0 +1,2 @@
|
|||
module LinksHelper
|
||||
end
|
21
app/models/link.rb
Normal file
21
app/models/link.rb
Normal 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
|
2
app/views/home/index.html.erb
Normal file
2
app/views/home/index.html.erb
Normal 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
2
app/views/links/0
Normal file
|
@ -0,0 +1,2 @@
|
|||
[1m[37m invoke[0m active_record
|
||||
[1m[32m create[0m db/migrate/20120503045327_change_column.rb
|
2
app/views/links/index.html.erb
Normal file
2
app/views/links/index.html.erb
Normal file
|
@ -0,0 +1,2 @@
|
|||
<h1>Links#index</h1>
|
||||
<p>Find me in app/views/links/index.html.erb</p>
|
|
@ -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.
|
||||
|
||||
|
|
11
db/migrate/20120503030309_create_links.rb
Normal file
11
db/migrate/20120503030309_create_links.rb
Normal 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
|
5
db/migrate/20120503043353_add_visits_to_link.rb
Normal file
5
db/migrate/20120503043353_add_visits_to_link.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
class AddVisitsToLink < ActiveRecord::Migration
|
||||
def change
|
||||
add_column :links, :visits, :integer
|
||||
end
|
||||
end
|
9
db/migrate/20120503051402_set_default_visits.rb
Normal file
9
db/migrate/20120503051402_set_default_visits.rb
Normal 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
25
db/schema.rb
Normal 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
11
test/fixtures/links.yml
vendored
Normal 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
|
9
test/functional/home_controller_test.rb
Normal file
9
test/functional/home_controller_test.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
require 'test_helper'
|
||||
|
||||
class HomeControllerTest < ActionController::TestCase
|
||||
test "should get index" do
|
||||
get :index
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
end
|
9
test/functional/links_controller_test.rb
Normal file
9
test/functional/links_controller_test.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
require 'test_helper'
|
||||
|
||||
class LinksControllerTest < ActionController::TestCase
|
||||
test "should get index" do
|
||||
get :index
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
end
|
4
test/unit/helpers/home_helper_test.rb
Normal file
4
test/unit/helpers/home_helper_test.rb
Normal file
|
@ -0,0 +1,4 @@
|
|||
require 'test_helper'
|
||||
|
||||
class HomeHelperTest < ActionView::TestCase
|
||||
end
|
4
test/unit/helpers/links_helper_test.rb
Normal file
4
test/unit/helpers/links_helper_test.rb
Normal file
|
@ -0,0 +1,4 @@
|
|||
require 'test_helper'
|
||||
|
||||
class LinksHelperTest < ActionView::TestCase
|
||||
end
|
7
test/unit/link_test.rb
Normal file
7
test/unit/link_test.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
require 'test_helper'
|
||||
|
||||
class LinkTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
Loading…
Reference in a new issue