Rails 4: having a proxy model to combine multiple

2019-05-10 04:19发布

I am trying my hand on rails (4). I have done some Sinatra earlier.

I have a signup form, in which user can fill out his organization name, address and his own name, password etc. I have two tables - Users and Organizations, those table get populated with Signup data. So, I have two active records model users and organizations. My controllers looks as follows:

class SignupsController < ApplicationController

  # GET /signup
  def new
    # show html form
  end

  # POST /signup
  def create
    @signup = Registration.register(signup_params)
    respond_to do |format|
      if @signup.save
        format.html { redirect_to @signup, notice: 'Signup was successfully created.' }
      else
        format.html { render action: 'new' }
      end
    end
  end

  private
    # Never trust parameters from the scary internet, only allow the white list through.
    def signup_params
      params[:signup]
    end
end

I do not have any Registration model (table in db). What I am looking for such Registration model (should I call it model?) where I can have something like:

class Registration
  def self.register params
    o = Organization.new params
    u = User.new o.id, params
    self.send_welcome_email params[:email]
  end

  def send_welcome_email email
    #send email here
  end
end

1) So where should I keep this Registration class?

2) Is this a correct approach for such situation? If not, then what is the best way to do it?

3) Having such class will effect running any unit tests?

4) I see there is file, signup_helper.rb what is the use of that in SignupsController

3条回答
够拽才男人
2楼-- · 2019-05-10 04:28

To answer your questions:

1) I would move the Registration class to a Signup module (as it relates to the signup use case) in app/models:

# apps/models/signup/registration.rb
module Signup
  class Registration
    ...
  end
end

2) I like your approach. It's something I would do. It is not the "Rails way" but in my opinion it is superior.

3) No. Having a PORO (plain old ruby object) is currently fine for any unit tests and you can easily test it.

4) Helpers are an (in my opinion) old and obsolete way to share functionality between views (and controllers). Don't use them unless there is absolutely no other way (maybe some Library demands it ... ). It is alway better to use POROs like you did.

You don't have to include ActiveModel::Model unless you need it's functionality. E.g. validations. You can include individual pieces of its functionality in this case. E.g

include ActiveModel::Validations
查看更多
小情绪 Triste *
3楼-- · 2019-05-10 04:40

You can do include ActiveModel::Model in your model, and it will behave as a normal Model. You will be able to do validations, callbacks. Anything other than persisting to a database.

Have a look at this for more info.

查看更多
Luminary・发光体
4楼-- · 2019-05-10 04:42
class Registration
  include ActiveModel::Model

  def self.register params
    o = Organization.new params
    u = User.new o.id, params
    self.send_welcome_email params[:email]
  end

  def send_welcome_email email
    #send email here
  end
end
查看更多
登录 后发表回答