Created
December 12, 2016 20:36
-
-
Save PelagicDev/5bb7acb644c5a16e1a639aef281e5438 to your computer and use it in GitHub Desktop.
Rails Form & Service Objects using PSQL `:daterange`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TournamentsController < ApplicationController | |
# GET /tournaments/new | |
def new | |
@form = TournamentSubmissionForm.new | |
end | |
# POST /tournaments | |
def create | |
@form = TournamentSubmissionForm.from_params(params) | |
SubmitNewTournament.call(@form, current_user) do | |
on(:invalid) { render :new } | |
on(:ok) { redirect_to tournaments_url, notice: "Tournament was successfully submitted. We'll contact you when it's approved." } | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TournamentSubmissionForm < Rectify::Form | |
mimic :tournament | |
attribute :name, String | |
attribute :city, String | |
attribute :state, String | |
attribute :start_date, Date | |
attribute :end_date, Date | |
attribute :headquarters, String | |
attribute :target_species, String | |
attribute :web_url, String | |
attribute :user_role, Integer, default: :none | |
attribute :status, Integer, default: :pending | |
validates :user_role, :name, :city, :state, :start_date, | |
:end_date, :target_species, presence: true | |
validate :dates_logic_validation | |
def dates_logic_validation | |
if start_date < Time.zone.today | |
errors.add(:start_date, "must be greater or equal to today's date") | |
return false | |
elsif start_date > end_date | |
errors.add(:end_date, 'must be greater than start date') | |
return false | |
else | |
true | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SubmitNewTournament < Rectify::Command | |
def initialize(form, user) | |
@form = form | |
@user = user | |
end | |
def call | |
return broadcast(:invalid, @form) if @form.invalid? | |
transaction do | |
create_tournament | |
create_staff_member unless form.user_role == 'none' | |
end | |
broadcast(:ok) | |
end | |
private | |
attr_reader :form, :user | |
def create_tournament | |
@tournament = Tournament.create( | |
name: form.name, | |
city: form.city, | |
state: form.state, | |
tournament_dates: format_dates, | |
target_species: form.target_species, | |
web_url: form.web_url, | |
status: :pending | |
) | |
end | |
def create_staff_member | |
@tournament.staff_members.create(user: user, role: form.user_role) | |
end | |
def format_dates | |
"[#{form.start_date.to_date},#{form.end_date.to_date})" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment