-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: Event forms use form objects
I'd like to decouple the form more from the model in order to provide a better UX with form fields which don't exist on the model. A first step is getting the existing functionality working with form objects. Note that on Edit we print the dates rather than returning the date array from the model. Get rid of usage of date_array - this hasn't actually been what we use for making the listings for a while now, but it's still been used in parallel - I guess because I wasn't sure about the SwingDates approach (which I still think is kind of batshit crazy and a bad idea, but it does mostly work) Building the URL separately based on @event is a tradeoff: without this we need to somehow get the event ID into the form so that `form_with` can do its thing and infer which URL to use, but I couldn't find an approach for that which works well: - adding an extra initialisation parameter breaks the validation specs which are expecting to be able to `build` an activemodel instance with FactoryBot - we could add ID as an attribute and set it in the validation error case on update, but that feels awkward. Note that we need to monkeypatch Shoulda Matchers to run it without ActiveRecord. This is NOT a "responsible" way of monkeypatching, but I couldn't get it to work and all the examples I can find online are about monkeypatching instance methods not class methods. Raised an issue on the Shoulda Matchers repo here: thoughtbot/shoulda-matchers#1553
- Loading branch information
Showing
30 changed files
with
525 additions
and
203 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateEventForm | ||
include ActiveModel::Model | ||
include ActiveModel::Attributes | ||
|
||
attribute :title, :string | ||
attribute :url, :string | ||
attribute :venue_id, :integer | ||
attribute :event_type, :string | ||
|
||
attribute :has_social, :boolean | ||
attribute :social_organiser_id, :integer | ||
|
||
attribute :has_class, :boolean | ||
attribute :has_taster, :boolean | ||
attribute :class_style, :string | ||
attribute :course_length, :string | ||
attribute :class_organiser_id, :integer | ||
|
||
attribute :frequency, :integer | ||
attribute :day, :string | ||
attribute :dates, :string | ||
attribute :cancellations, :string | ||
attribute :first_date, :string | ||
attribute :last_date, :string | ||
|
||
def self.model_name | ||
Event.model_name | ||
end | ||
|
||
validates :url, presence: true, uri: true | ||
validates :venue_id, presence: true | ||
validates :event_type, presence: true | ||
validates :frequency, presence: true | ||
validates :course_length, numericality: { only_integer: true, greater_than: 0, allow_blank: true } | ||
|
||
validates_with ValidSocialOrClass | ||
validates_with ValidWeeklyEvent | ||
|
||
def action | ||
"Create" | ||
end | ||
|
||
def weekly? | ||
frequency == 1 | ||
end | ||
|
||
def infrequent? | ||
return false if frequency.nil? | ||
|
||
frequency.zero? || frequency >= 4 | ||
end | ||
|
||
def has_social? # rubocop:disable Naming/PredicateName | ||
!!has_social | ||
end | ||
|
||
def has_class? # rubocop:disable Naming/PredicateName | ||
!!has_class | ||
end | ||
|
||
def to_h | ||
attributes.merge( | ||
"dates" => DatesStringParser.new.parse(dates), | ||
"cancellations" => DatesStringParser.new.parse(cancellations) | ||
) | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
class EditEventForm < CreateEventForm | ||
include ActiveModel::Model | ||
include ActiveModel::Attributes | ||
|
||
class << self | ||
def from_event(event) | ||
new( | ||
{ | ||
title: event.title, | ||
url: event.url, | ||
venue_id: event.venue_id, | ||
event_type: event.event_type, | ||
|
||
has_social: event.has_social, | ||
social_organiser_id: event.social_organiser_id, | ||
|
||
has_class: event.has_class, | ||
has_taster: event.has_taster, | ||
class_style: event.class_style, | ||
course_length: event.course_length, | ||
class_organiser_id: event.class_organiser_id, | ||
|
||
frequency: event.frequency, | ||
day: event.day, | ||
dates: event.print_dates, | ||
cancellations: event.print_cancellations, | ||
first_date: event.first_date, | ||
last_date: event.last_date | ||
} | ||
) | ||
end | ||
end | ||
|
||
def action | ||
"Update" | ||
end | ||
|
||
def persisted? | ||
true | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
class OrganiserEditEventForm | ||
include ActiveModel::Model | ||
include ActiveModel::Attributes | ||
|
||
class << self | ||
def from_event(event) | ||
new( | ||
{ | ||
venue_id: event.venue_id, | ||
dates: event.print_dates, | ||
cancellations: event.print_cancellations, | ||
last_date: event.last_date | ||
} | ||
) | ||
end | ||
end | ||
|
||
attribute :venue_id, :integer | ||
attribute :dates, :string | ||
attribute :cancellations, :string | ||
attribute :last_date, :string | ||
|
||
def self.model_name | ||
Event.model_name | ||
end | ||
|
||
validates :venue_id, presence: true | ||
|
||
def to_h | ||
attributes.merge( | ||
"dates" => DatesStringParser.new.parse(dates), | ||
"cancellations" => DatesStringParser.new.parse(cancellations) | ||
) | ||
end | ||
|
||
def persisted? | ||
true | ||
end | ||
end |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.