Last active
March 2, 2016 16:00
-
-
Save ezrarush/d906d9bc0e66987acd80 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ===================================================================================================== | |
# Template for generating a callback example | |
# ===================================================================================================== | |
# Requirements: | |
# ------------- | |
# | |
# * Ruby >= 1.9.3 | |
# * Rails >= 4 | |
# Usage: | |
# ------ | |
# | |
# $ rails new callback-example --skip-bundle --template callback-example.rb | |
# | |
# ===================================================================================================== | |
# ----- Use Thin ---------------------------------------------------------------------------------- | |
begin | |
require 'thin' | |
gem 'thin' | |
rescue LoadError | |
end | |
# ----- Remove CoffeeScript, Sass and "all that jazz" --------------------------------------------- | |
comment_lines 'Gemfile', /gem 'coffee/ | |
comment_lines 'Gemfile', /gem 'sass/ | |
comment_lines 'Gemfile', /gem 'uglifier/ | |
uncomment_lines 'Gemfile', /gem 'therubyracer/ | |
# ----- Disable asset logging in development ------------------------------------------------------ | |
environment 'config.assets.logger = false', env: 'development' | |
gem 'quiet_assets', group: "development" | |
# ----- Install gems ------------------------------------------------------------------------------ | |
run "bundle install" | |
# ----- Generate a resource to test around_update callback ------------------------------------------------------- | |
generate :scaffold, "post name:string user:belongs_to" | |
generate :scaffold, "user name:string" | |
generate :scaffold, "profile name:string user:belongs_to" | |
route "root to: 'posts#index'" | |
rake "db:migrate" | |
# ----- Add Associations ------------------- | |
run "rm -f app/models/user.rb" | |
file 'app/models/user.rb', <<-CODE | |
class User < ActiveRecord::Base | |
has_one :profile | |
has_many :posts | |
end | |
CODE | |
# ----- Add Callback ------------------- | |
run "rm -f app/models/profile.rb" | |
file 'app/models/profile.rb', <<-CODE | |
class Profile < ActiveRecord::Base | |
belongs_to :user | |
around_update :test_method | |
private | |
def test_method | |
print "beforeZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ" | |
yield | |
print "afterZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ" | |
end | |
end | |
CODE | |
# ----- Add #duplicate_post to post model ------------------- | |
run "rm -f app/models/post.rb" | |
file 'app/models/post.rb', <<-CODE | |
class Post < ActiveRecord::Base | |
belongs_to :user | |
def duplicate_post | |
duplicate = self.dup | |
duplicate.save(:validate => false) | |
return duplicate | |
end | |
end | |
CODE | |
# ----- Add #duplicate_post to post model ------------------- | |
run "rm -f app/controllers/posts_controller.rb" | |
file 'app/controllers/posts_controller.rb', <<-'CODE' | |
class PostsController < ApplicationController | |
before_action :set_post, only: [:show, :edit, :update, :destroy, :duplicate] | |
# GET /posts | |
# GET /posts.json | |
def index | |
@posts = Post.all | |
end | |
# GET /posts/1 | |
# GET /posts/1.json | |
def show | |
end | |
# GET /posts/new | |
def new | |
@post = Post.new | |
end | |
# GET /posts/1/edit | |
def edit | |
end | |
# POST /posts | |
# POST /posts.json | |
def create | |
@post = Post.new(post_params) | |
respond_to do |format| | |
if @post.save | |
format.html { redirect_to @post, notice: 'Post was successfully created.' } | |
format.json { render :show, status: :created, location: @post } | |
else | |
format.html { render :new } | |
format.json { render json: @post.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# PATCH/PUT /posts/1 | |
# PATCH/PUT /posts/1.json | |
def update | |
respond_to do |format| | |
if @post.update(post_params) | |
format.html { redirect_to @post, notice: 'Post was successfully updated.' } | |
format.json { render :show, status: :ok, location: @post } | |
else | |
format.html { render :edit } | |
format.json { render json: @post.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# DELETE /posts/1 | |
# DELETE /posts/1.json | |
def destroy | |
@post.destroy | |
respond_to do |format| | |
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' } | |
format.json { head :no_content } | |
end | |
end | |
def duplicate | |
duplicate_post = @post.duplicate_post | |
if duplicate_post.save(:validate => false) | |
redirect_to post_url(duplicate_post) | |
else | |
raise RuntimeError, "failed: #{duplicate_post.errors}" | |
end | |
end | |
private | |
# Use callbacks to share common setup or constraints between actions. | |
def set_post | |
@post = Post.find(params[:id]) | |
end | |
# Never trust parameters from the scary internet, only allow the white list through. | |
def post_params | |
params.require(:post).permit(:name, :user_id) | |
end | |
end | |
CODE | |
# ----- Add Tests ------------------- | |
run "rm -f test/models/profile_test.rb" | |
file 'test/models/profile_test.rb', <<-CODE | |
require 'test_helper' | |
class ProfileTest < ActiveSupport::TestCase | |
test "updates trigger around_update callback #test_method" do | |
assert true | |
end | |
end | |
CODE | |
run "rm -f test/models/post_test.rb" | |
file 'test/models/post_test.rb', <<-CODE | |
require 'test_helper' | |
class PostTest < ActiveSupport::TestCase | |
test "#duplicate_post should not trigger Profile's around_update callback #test_method" do | |
assert true | |
end | |
end | |
CODE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment