Skip to content

Commit a348afb

Browse files
author
plugin73
committed
aded some state_machine
1 parent a5deae9 commit a348afb

File tree

10 files changed

+215
-60
lines changed

10 files changed

+215
-60
lines changed

.idea/misc.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/rails_new_lab.iml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 177 additions & 58 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/controllers/posts_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ def edit
3737
@post = Post.find(params[:id])
3838
end
3939

40+
def change_state
41+
@state = "123"
42+
end
43+
4044
# POST /posts
4145
# POST /posts.json
4246
def create

app/models/post.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
11
class Post < ActiveRecord::Base
22
attr_accessible :body, :description, :published_at, :title
33
validates :title, :presence => true
4+
5+
state_machine initial: :new do
6+
event :accept do
7+
transition [:new, :rejected] => :accepted
8+
end
9+
10+
event :reject do
11+
transition :new => :rejected
12+
end
13+
14+
event :start do
15+
transition :accepted => :started
16+
end
17+
18+
event :finish do
19+
transition :started => :finished
20+
end
21+
end
422
end

app/views/layouts/application.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html>
33
<head>
44
<title>RailsNewLab</title>
5-
<%= stylesheet_link_tag "application", :media => "all" %>
5+
<%= stylesheet_link_tag "custom", :media => "all" %>
66
<%= javascript_include_tag "application" %>
77
<%= csrf_meta_tags %>
88
</head>

app/views/posts/index.html.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,22 @@
66
<th>Title</th>
77
<th>Description</th>
88
<th>Body</th>
9+
<th>State</th>
910
<th></th>
1011
<th></th>
1112
<th></th>
1213
</tr>
1314

15+
16+
1417
<% @posts.each do |post| %>
18+
<% post_change_state_path(post) %>
1519
<tr>
1620
<td><%= post.published_at %></td>
1721
<td><%= post.title %></td>
1822
<td><%= post.description %></td>
1923
<td><%= post.body %></td>
24+
<td><%= post.state %></td>
2025
<td><%= link_to 'Show', post %></td>
2126
<td><%= link_to 'Edit', edit_post_path(post) %></td>
2227
<td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>

config/routes.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
RailsNewLab::Application.routes.draw do
2-
resources :posts
2+
resources :posts do
3+
put :change_state
4+
end
5+
36

47

58
# The priority is based upon order of creation:

db/migrate/20121109174650_create_posts.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ def change
55
t.string :title
66
t.string :description
77
t.text :body
8+
t.string :state
89

910
t.timestamps
1011
end

db/schema.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
t.string "title"
1919
t.string "description"
2020
t.text "body"
21+
t.string "state"
2122
t.datetime "created_at", :null => false
2223
t.datetime "updated_at", :null => false
2324
end

0 commit comments

Comments
 (0)