You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Ruby PORO quite confuse me sometimes, let talk a look at this example:
classTardisMotoForm# Include the ActiveModelincludeActiveModel::ModelincludeActiveModel::Validations::Callbacks# Define a custom constantPASSWORD_FORMAT=/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])/# Define attr_accessor, so we can expose these attributes to the outside world# take a look more on attr_reader and attr_accessorattr_accessor:user,:email,:password,:phone_number,:transfer_type,:params,:taken_email,:params# Use the validation of modelvalidate:email_uniquenessvalidates:password,presence: true,format: {with: PASSWORD_FORMAT},length: {in: Devise.password_length},if: :transfer_by_email?validates:email,email: {validate_mx: false}phony_normalize:phone_numbervalidates:phone_number,phony_plausible: true# Define a custom method for the outside world can invoke# For example, in the controller# if @tardis_moto_form.valid_params?(filtered_fun_params)# redirect_to somewhere_path# else# render :new# enddefvalid_params?(params)assign_attributes(filter_params(params))# where the assign_attribute come from?valid?end# In the controller# if @tardis_moto_form.save(filtered_fun_params, resource) && resource.persisted?# enddefsave(attributes={},user=nil)@user=user# What is the user here? - it will assign to the attr user above@params=attributesuser.errors.empty?endprivate# Model validation callbackdeftransfer_by_email?transfer_type.to_sym == :emailend# Model validation call backdefemail_uniquenessreturnunlessUser.exists?(email: email)@taken_email=trueerrors.add(:email,I18n.t('activerecord.errors.models.transfer_form.taken_email'))end# Sanitize filter params before assign attribute deffilter_params(params){email: params[:email],password: params[:password],phone_number: params[:phone_number],}endend
Here we have three user text: :user: define user attribute of the class, which can be access from the outside world. @user: for assign user into attribute :user as define in attr_accessor. user: method params will assign to instance variable @user.
Ruby PORO quite confuse me sometimes, let talk a look at this example:
Reference:
The text was updated successfully, but these errors were encountered: