2014-01-14 33 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