Skip to content

Instantly share code, notes, and snippets.

@fidothe
Created June 27, 2016 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fidothe/f2908669b1459034cea004204abceede to your computer and use it in GitHub Desktop.
Save fidothe/f2908669b1459034cea004204abceede to your computer and use it in GitHub Desktop.
application process
# The code smell: disjoint radio buttons reveal that some values that
# should have hung together didn't, and that there were also dependent
# fields (the data protection checkbox and the application form link)
# which were hanging around and being a little bit problematic
#
# A solution is to use one field that can hold several values, unlike the
# boolean which can only hold two.
#
# We can also introduce a VALUE OBJECT that represents this field and can
# also tell us which (if any) of the dependent fields are needed.
#
# We can also extract the complex bits of the validations to a module so
# that they're more contained and we isolate the interactions with the
# Value objects.
module ApplicationProcess
module Validator
def self.data_protection_confirmation
{
if: ->(event) { Value.new(event).requires_data_protection? },
allow_nil: false
}
end
def self.application_link
{
if: ->(event) { Value.new(event).requires_application_link? },
presence: true,
format: { with: /(http|https):\/\/.+\..+/ }
}
end
def self.value
{
inclusion: {
in: Value::VALUES
},
presence: true
}
end
end
class Value
VALUES = %w{selection_by_travis selection_by_organizer application_by_organizer}
attr_reader :event
def initialize(event)
@event = event
end
def value
event.application_process
end
def requires_data_protection?
value == 'selection_by_organizer'
end
def requires_application_link?
value == 'application_by_organizer'
end
def valid?
VALUES.include?(value)
end
end
end
class Event < ActiveRecord::Base
# application_process :string
validates :application_process, ApplicationProcess::Validator.value
validates_acceptance_of :data_protection_confirmation, ApplicationProcess::Validator.data_protection_confirmation
validates :application_link, ApplicationProcess::Validator.application_link
end
# For viele points:
class Event < ActiveRecord::Base
include ApplicationProcess::Validator
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment