2014-01-14 32 views
0

我已經在我的RoR應用程序中設置了Action Mailer,並且我希望在其「MatchCenter」頁面的內容更改時向用戶發送電子郵件。如何定義監控頁面內容更改的方法?

根據用戶通過微博提供的標準,MatchCenter顯示使用eBay API的項目。這意味着由於外部來源eBay而導致結果不斷變化,而不是用戶的任何行爲。

我正在考慮創建一個MatchCenter控制器並在其中定義一個方法來監視MatchCenter頁面的變化。如果發生變化,我會打電話給UserMailer.matchcenter_notification(@user).deliver

我遇到困難的部分是放在MatchCenter控制器方法中,該方法將檢測由於外部源而導致的頁面更改。另外,如果有更好的方法來做到這一點,我很樂意聽到它。所有相關的代碼如下。謝謝!

users_controller.rb:

class UsersController < ApplicationController 
    before_action :signed_in_user, only: [:edit, :update] 
    before_action :correct_user, only: [:edit, :update] 
    before_action :admin_user,  only: [:index, :destroy] 

    def show 
    @user = User.find(params[:id]) 
    @microposts = @user.microposts.paginate(page: params[:page]) 
    end 

    def new 
    @user = User.new 
    end 

    def index 
    @users = User.paginate(page: params[:page])  
    end 

    def create 
    @user = User.new(user_params) 
    if @user.save 
     sign_in @user 
     flash[:success] = "Welcome to the MatchCenter Alpha Test!"  
     redirect_to root_path 
    else 
     render 'new' 
    end 
    end 


    def show 
    @user = User.find(params[:id]) 

    end 


    def admin_user 
     redirect_to(root_url) unless current_user.admin? 
    end 

    def edit 
    @user = User.find(params[:id]) 
    end 

    def destroy 
    User.find(params[:id]).destroy 
    flash[:success] = "User deleted." 
    redirect_to users_url 
    end 

    def update 
    @user = User.find(params[:id]) 
    if @user.update_attributes(user_params) 
     flash[:success] = "Profile updated" 
     redirect_to @user 
    else 
     render 'edit' 
    end 

    end 


    private 

    def user_params 
     params.require(:user).permit(:name, :email, :password, 
            :password_confirmation) 
    end 

    # Before filters 

    def correct_user 
     @user = User.find(params[:id]) 
     redirect_to(root_url) unless current_user?(@user) 
    end 
end 

microposts_controller.rb:

class MicropostsController < ApplicationController 
    before_action :signed_in_user 
    before_action :correct_user, only: :destroy 

    def new 
    @micropost = current_user.microposts.build 
    end 

    def create 
    @micropost = current_user.microposts.build(micropost_params) 
    if @micropost.save 
     flash[:success] = "Sweet! The item has been added to your watch list, and we'll notify you whenever matches are found." 
     redirect_to buy_path 
    else 
     render 'static_pages/home' 
    end 
    end 

    def destroy 
    @micropost.destroy 
    redirect_to buy_path 
    end 

    private 

    def micropost_params 
     params.require(:micropost).permit(:keyword, :min, :max, :condition) 
    end 

    def correct_user 
     @micropost = current_user.microposts.find_by(id: params[:id]) 
     redirect_to root_url if @micropost.nil? 
    end 
end 

回答

0

如果MatchCenter是當易趣檢測到變化,這被修改的模型,那麼你可以很容易地實現這個使用觀測。

如果這是一個pre-rails-4應用程序,則觀察者會被烘烤。使用導軌4時,它們已經是moved into a plugin。在新創建的文件觀察員

rails g observer MatchCenter 

然後,定義它是什麼觀察和什麼用它做:

創建觀察者

class MatchCenterObserver < ActiveRecord::Observer 
    observe MatchCenter 

    def notify_users(match_center) 
    # 
    # Do any required notifications etc here 
    # 
    end 
    alias_method :after_create, :notify_users 
    alias_method :after_update, :notify_users 
    alias_method :after_destroy, :notify_users 
end