Sha256: b139e87b77a91905764b524d9c6c6e1b135004690b539167ab13b18786ee6b70

Contents?: true

Size: 1.06 KB

Versions: 10

Compression:

Stored size: 1.06 KB

Contents

module Jobshop
  class Registration < VirtualRecord
    memo_attr(:user) { team.users.build(user_params) }
    memo_attr(:team) {
      record = ::Jobshop::Team.find(params[:team_id])

      record.assign_attributes(team_params)

      record.singleton_class.class_eval do
        validates :name, presence: true, on: :update, if: :name_changed?
      end

      record
    }

    def save
      return false unless valid?

      result = ActiveRecord::Base.transaction do
        user.save!
        team.update!(owner: user,
                     registration_token: nil,
                     registration_token_sent_at: nil)
      end

      result != false
    end

    private

    def registration_params
      params.fetch(:registration, ActionController::Parameters.new)
    end

    def team_params
      registration_params.fetch(:team, ActionController::Parameters.new)
        .permit(:name)
    end

    def user_params
      registration_params.fetch(:user, ActionController::Parameters.new)
        .permit(:email, :password, :password_confirmation)
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
jobshop-0.0.113 app/models/jobshop/registration.rb
jobshop-0.0.109 app/models/jobshop/registration.rb
jobshop-0.0.107 app/models/jobshop/registration.rb
jobshop-0.0.101 app/models/jobshop/registration.rb
jobshop-0.0.67 app/models/jobshop/registration.rb
jobshop-0.0.61 app/models/jobshop/registration.rb
jobshop-0.0.59 app/models/jobshop/registration.rb
jobshop-0.0.53 app/models/jobshop/registration.rb
jobshop-0.0.41 app/models/jobshop/registration.rb
jobshop-0.0.37 app/models/jobshop/registration.rb