Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

built the article model for arakla_world #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class ArticlesController < ApplicationController

def index
@articles = Article.alphabetical.paginate(:page => params[:page]).per_page(10)
end

def show
@article = Article.find(params[:id])
end


def new
@article = Article.new
end


def edit
@article = Article.find(params[:id])
end


def create
@article = Article.new(params[:article])
if @article.save
flash[:notice] = 'Article was successfully created.'
redirect_to article_path(@article)
else
render :action => "new"
end
end


def update
@article = Article.find(params[:id])
if @article.update_attributes(params[:article])
flash[:notice] = 'Article was successfully updated.'
redirect_to article_path(@article)
else
render :action => "edit"
end
end


def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path
end
end
10 changes: 10 additions & 0 deletions app/models/article.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Article < ActiveRecord::Base
# relationship
belongs_to :category
# validation
validates_presence_of :title, :content

# scope
scope :alphabetical, -> {order('title')}
scope :active, -> {where(active: true)}
end
1 change: 1 addition & 0 deletions app/models/category.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Category < ActiveRecord::Base
has_many :photos
has_many :articles

scope :active, where('active = ?', true)
scope :alphabetical, order('name')
Expand Down
15 changes: 15 additions & 0 deletions app/views/articles/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<%= simple_form_for @article, :html => { :class => 'form-horizontal' } do |f| %>
<fieldset>
<legend><%= controller.action_name.capitalize %> Article</legend>

<%= f.input :title %>
<%= f.input :content %>
<%= f.input :category_id, :collection => Category.active.alphabetical, :include_blank => true %>
<%= f.input :active %>

<div class="form-actions">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to 'Cancel', articles_path, :class => 'btn' %>
</div>
</fieldset>
<% end %>
1 change: 1 addition & 0 deletions app/views/articles/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render :partial => 'form' %>
25 changes: 25 additions & 0 deletions app/views/articles/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<h2>Articles</h2>
<table class="table table-striped">
<thead>
<tr>
<th>Title</th>
<th>Category</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<% @articles.each do |article| %>
<tr>
<td><%= link_to article.title, article_path(article) %></td>
<td><%= article.category.name %></td>
<td>
<%= link_to 'Edit', edit_article_path(article), :class => 'btn btn-mini' %>
<%= link_to 'Destroy', article_path(article), :method => :delete, :confirm => 'Are you sure?', :class => 'btn btn-mini btn-danger' %>
</td>
</tr>
<% end %>
</tbody>
</table>
<%= will_paginate @articles, :previous_label => "Previous&nbsp;", :next_label => "&nbsp;Next" %>

<%= link_to 'New Article', new_article_path, :class => 'btn btn-primary' %>
1 change: 1 addition & 0 deletions app/views/articles/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render :partial => 'form' %>
18 changes: 18 additions & 0 deletions app/views/articles/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<h2><%= @article.title %></h2>

<p>
<%= @article.content %>
</p>

<p>&nbsp;</p>

<p>
<b>Category</b>:
<%= @article.category.name %>
</p>

<div class="form-actions">
<%= link_to 'Back', articles_path, :class => 'btn' %>
<%= link_to 'Edit', edit_article_path(@article), :class => 'btn' %>
<%= link_to 'Delete', article_path(@article), :method => 'delete', :confirm => 'Are you sure?', :class => 'btn btn-danger' %>
</div>
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
resources :categories
resources :proverbs
resources :photos
resources :articles
get 'home' => 'home#index', :as => :home
root :to => 'home#index'

Expand Down
Binary file modified db/development.sqlite3
Binary file not shown.
12 changes: 12 additions & 0 deletions db/migrate/20140422002752_create_articles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateArticles < ActiveRecord::Migration
def change
create_table :articles do |t|
t.string :title
t.text :content
t.integer :category_id
t.boolean :active

t.timestamps
end
end
end
37 changes: 23 additions & 14 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,41 @@
# 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.
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(:version => 20120412125204) do
ActiveRecord::Schema.define(version: 20140422002752) do

create_table "categories", :force => true do |t|
create_table "articles", force: true do |t|
t.string "title"
t.text "content"
t.integer "category_id"
t.boolean "active"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "categories", force: true do |t|
t.string "name"
t.boolean "active", :default => true
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.boolean "active", default: true
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "photos", :force => true do |t|
create_table "photos", force: true do |t|
t.string "caption"
t.integer "category_id"
t.boolean "active", :default => true
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.boolean "active", default: true
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image"
end

create_table "proverbs", :force => true do |t|
create_table "proverbs", force: true do |t|
t.string "klingon"
t.string "translation"
t.boolean "active", :default => true
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.boolean "active", default: true
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

end
2 changes: 1 addition & 1 deletion test/fixtures/articles.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
title: MyString
Expand Down
7 changes: 7 additions & 0 deletions test/models/article_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class ArticleTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end