开发者

RoR : Polymorphic Controllers

I have an existing site that has a bunch of different models and controllers. I am currently integrating Twilio's services into this site. Twilio allows you to supply a url that will be called when a user interacts with your phone number using their phone. Unfortunately, there is only one url that you can provide to Twilio and then all the parsing is done on your end.

So, now I have a twilio controller which parses the user's data and decides what they are trying to do.

Everything the user may be trying to do via their phone can be done on the website already, but now they have the option to use their phone when on the go. If they text my number "create group foo" then the site will try to create the group accordingly. My issue is that I already have a 开发者_高级运维groups controller that knows how to create groups and has the appropriate before_filters to make sure that the user has permission to do so, amongst other things.

Is there a way for the twilio controller to parse the request and then "forward" it over to the proper controller in some way? I'd rather not have the twilio controller duplicate all of the code and filters that are in every other controller and some of that stuff doesn't feel right to be shoved into the models.

I'm somewhat new to rails in general, so I'm open to any suggestion. I'm hoping there's some design pattern out there that fits my use case and I'm willing to refactor my whole project for the correct solution.


I think there are a couple of things you can do. If you don't have to respond in a certain format, then you can simply redirect the request with the appropriately formatted parameters. For example:

class TwilioController
  def create
    if params[:twilio_action] == 'create group'
      redirect_to create_group_path(:id => params[:group_id], :number => params[:number])
    end
  end
end

There's a good chance that you'll have problems with authentication though, because the twilio api will not be sending and receiving cookies for you, so you will not have an authenticated user. If this is the case it will be best to put all your shared code in the model and handle cookie authentication with your GroupsController and phone number authentication with your TwilioController. For example:

class TwilioController
  def create
    if params[:twilio_action] == 'create group'
      if can_create_group?(params[:phone_number])
        Group.create(:id => params[:group_id])
      end
    end
  end
end

It's always best to put your business logic in your model, but if you do actually have a function you want to share within two controllers you can always create a module to do that as well:

module GroupControllerActions
  def create_group user
    Group.create(params[:group].merge({:user => user}))
  end
end

class TwilioController
  include GroupControllerActions

  def create
    if params[:twilio_action] == 'create group'
      create_group(User.find_by_number(params[:phone_number]))
    end
  end
end

class GroupsController
  def create
    create_group(current_user)
  end
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜