Permalink
Browse files

Troubles with state_machine

  • Loading branch information...
1 parent 1099a94 commit e0805fffea278647d5f53293312e84f51fa8e132 @vaski committed Sep 13, 2012
View
@@ -3,6 +3,7 @@ source 'https://rubygems.org'
gem 'rails', '3.2.8'
gem 'devise'
gem 'cancan'
+gem 'state_machine'
gem 'jquery-rails'
gem 'faker'
gem 'bootstrap-sass'
View
@@ -146,6 +146,7 @@ GEM
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
sqlite3 (1.3.6)
+ state_machine (1.1.2)
thor (0.16.0)
tilt (1.3.3)
treetop (1.4.10)
@@ -180,5 +181,6 @@ DEPENDENCIES
sass-rails (~> 3.2.3)
simple_form
sqlite3
+ state_machine
uglifier (>= 1.0.3)
will_paginate (~> 3.0)
@@ -9,7 +9,7 @@ def create
@ad = current_user.ads.build(params[:ad])
if @ad.save
flash[:success] = "Ad created!"
- redirect_to current_user
+ render 'show'
else
flash.now[:error] = "Ad not created!"
render 'new'
@@ -19,7 +19,7 @@ def create
def update
if @ad.update_attributes(params[:ad])
flash[:success] = "Ad updated!"
- redirect_to current_user
+ render 'show'
else
flash.now[:error] = "Ad not updated!"
render 'edit'
@@ -10,7 +10,7 @@ def show
end
def destroy
- User.find(params[:id]).destroy
+ @user.destroy
flash[:success] = "User destroyed!"
redirect_to users_path
end
View
@@ -8,6 +8,7 @@
# user_id :integer
# created_at :datetime not null
# updated_at :datetime not null
+# state :string(255)
#
class Ad < ActiveRecord::Base
@@ -23,4 +24,35 @@ class Ad < ActiveRecord::Base
validates :user_id, presence: true
default_scope order: 'ads.updated_at DESC'
+
+ before_save do |ad|
+ ad.state = 'draft' if ad.title_changed? || ad.description_changed?
+ end
+
+ state_machine :state, initial: :draft do
+
+ event :verify do
+ transition draft: :verified
+ end
+
+ event :approve do
+ transition verified: :approved
+ end
+
+ event :reject do
+ transition verified: :rejected
+ end
+
+ event :publish do
+ transition approved: :published
+ end
+
+ event :archive do
+ transition published: :archived
+ end
+
+ event :refresh do
+ transition all - [:draft] => :draft
+ end
+ end
end
@@ -2,7 +2,9 @@
<td>
<h3><%= link_to ad.title, ad_path(ad) %></h3>
<p><%= ad.description %></p>
- <p>Created <%= time_ago_in_words(ad.created_at) %> ago</p>
+ <p>
+ <span class="label"><%= ad.state %></span>
+ Created <%= time_ago_in_words(ad.created_at) %> ago</p>
<p>
<% if can? :update, ad %>
<%= link_to "edit", edit_ad_path(ad), class: 'btn btn-mini btn-primary' %>
@@ -5,6 +5,7 @@
<h1><%= @ad.title %></h1>
<p><%= @ad.description %></p>
<p>
+ <span class="label"><%= @ad.state %></span>
Created by
<% if can? :read, @users %>
<%= link_to @ad.user.name, user_path(@ad.user) %> <%= time_ago_in_words(@ad.created_at) %>
@@ -15,6 +16,7 @@
</p>
<p>
<% if can? :update, @ad %>
+ <%= link_to "verify", ad_path(@ad, state: @ad.verify), class: 'btn btn-success' %>
<%= link_to "edit", edit_ad_path(@ad), class: "btn btn-primary" %>
<% end %>
<% if can? :destroy, @ad %>
@@ -0,0 +1,5 @@
+class AddStateToAds < ActiveRecord::Migration
+ def change
+ add_column :ads, :state, :string
+ end
+end
View
@@ -11,14 +11,15 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120911165304) do
+ActiveRecord::Schema.define(:version => 20120912161147) do
create_table "ads", :force => true do |t|
t.string "title"
t.text "description"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
+ t.string "state"
end
add_index "ads", ["user_id", "created_at"], :name => "index_ads_on_user_id_and_created_at"
View
@@ -8,6 +8,7 @@
# user_id :integer
# created_at :datetime not null
# updated_at :datetime not null
+# state :string(255)
#
require 'spec_helper'

0 comments on commit e0805ff

Please sign in to comment.