-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Marketplace
: Sprout Delivery::Window
object
- #1185 So, the `delivery_window` field which can either be a plain string or a particular date or even a date range is a much more sophisticated object. I took some time last night to explore what it could look like to use `ActiveModel::Type`s to create a value object which can take responsibility for representing the data more appropriately. That said, I think `store_model` will be better ™️, but I didn't want to try to dig into that just yet.
- Loading branch information
Showing
14 changed files
with
103 additions
and
18 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
9 changes: 9 additions & 0 deletions
9
app/furniture/marketplace/cart/deliveries/_window_field.html.erb
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,9 @@ | ||
<%- delivery = form.object%> | ||
<%- disabled = delivery.marketplace.delivery_window.present? %> | ||
<%- window = form.object.send(attribute) %> | ||
|
||
<%- if window.value.is_a?(DateTime) || delivery.marketplace.delivery_window.blank? %> | ||
<%= render "datetime_field", attribute: attribute, form: form, disabled?: disabled %> | ||
<%- else %> | ||
<%= render "text_field", attribute: attribute, form: form, disabled?: disabled %> | ||
<%- 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Marketplace | ||
class Delivery < Order | ||
attribute :delivery_window, ::Marketplace::Delivery::WindowType.new | ||
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,21 @@ | ||
class Marketplace | ||
class Delivery | ||
class Window < Model | ||
attr_writer :value | ||
|
||
def value | ||
return nil if @value.blank? | ||
|
||
DateTime.iso8601(@value) | ||
rescue Date::Error => _e | ||
@value | ||
end | ||
|
||
delegate :strftime, to: :value | ||
|
||
def eql?(other) | ||
value.eql?(other.value) | ||
end | ||
end | ||
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,13 @@ | ||
class Marketplace | ||
class Delivery | ||
class WindowType < ActiveRecord::Type::Value | ||
def deserialize(value) | ||
Window.new(value: value) | ||
end | ||
end | ||
|
||
def cast(value) | ||
Window.value(value: value) | ||
end | ||
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,5 @@ | ||
<%- if window.value.is_a?(DateTime) %> | ||
<%= l(window.value, format: :day_month_date_hour_minute) %> | ||
<%- else %> | ||
<%= window.value %> | ||
<%- 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
<%- disabled = local_assigns.fetch(:disabled?, false)%> | ||
<div> | ||
<%= form.label attribute %> | ||
<%= form.datetime_field attribute %> | ||
<%= form.datetime_field attribute, disabled: disabled %> | ||
<%= render partial: "error", locals: { model: form.object, attribute: attribute } %> | ||
</div> |
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,27 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe Marketplace::Delivery::Window do | ||
subject(:window) { described_class.new(value: value) } | ||
|
||
describe "#value" do | ||
subject(:deserialize) { window.value } | ||
|
||
context "when the value is blank" do | ||
let(:value) { "" } | ||
|
||
it { is_expected.to be_nil } | ||
end | ||
|
||
context "when the value is an iso8601 String" do | ||
let(:value) { DateTime.parse("2023-01-01 3:00pm").iso8601 } | ||
|
||
it { is_expected.to eql(DateTime.parse("2023-01-01 3:00pm")) } | ||
end | ||
|
||
context "when the value is a plain string" do | ||
let(:value) { "This Evening" } | ||
|
||
it { is_expected.to eql(value) } | ||
end | ||
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,11 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe Marketplace::Delivery::WindowType do | ||
subject(:type) { described_class.new } | ||
|
||
describe "#deserialize" do | ||
subject(:deserialize) { type.deserialize("words") } | ||
|
||
it { is_expected.to eql(Marketplace::Delivery::Window.new(value: "words")) } | ||
end | ||
end |