Skip to content

Commit

Permalink
Allow to set input_type on Datetime ingredient editor
Browse files Browse the repository at this point in the history
Since the Datetime ingredient stores date and time values
it should be possible to set the input_type to either

- time
- date
- datetime

(cherry picked from commit 02a69a6)

# Conflicts:
#	app/views/alchemy/ingredients/_datetime_editor.html.erb
#	spec/views/alchemy/ingredients/datetime_editor_spec.rb
  • Loading branch information
tvdeyen committed Sep 3, 2024
1 parent 6e74d2b commit c8e03fa
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/models/alchemy/ingredients/datetime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Ingredients
# A datetime value
#
class Datetime < Alchemy::Ingredient
allow_settings %i[date_format]
allow_settings %i[date_format input_type]

def value
ActiveRecord::Type::DateTime.new.cast(self[:value])
Expand Down
3 changes: 2 additions & 1 deletion app/views/alchemy/ingredients/_datetime_editor.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
datetime_editor, :value, {
name: datetime_editor.form_field_name,
id: datetime_editor.form_field_id,
value: datetime_editor.value
value: datetime_editor.value,
type: datetime_editor.settings[:input_type]
}
) %>
<% end %>
Expand Down
2 changes: 2 additions & 0 deletions spec/dummy/config/alchemy/elements.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@
- role: datetime
type: Datetime
hint: true
settings:
input_type: datetime
- role: file
type: File
hint: true
Expand Down
6 changes: 6 additions & 0 deletions spec/models/alchemy/ingredients/datetime_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
)
end

describe ".allowed_settings" do
it "sets allowed_settings" do
expect(described_class.allowed_settings).to eq([:date_format, :input_type])
end
end

describe "value" do
subject { datetime_ingredient.value }

Expand Down
2 changes: 1 addition & 1 deletion spec/views/alchemy/ingredients/datetime_editor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@

it "renders a datepicker" do
render element_editor
expect(rendered).to have_css('input[type="text"][data-datepicker-type="date"].date')
expect(rendered).to have_css('input[type="text"][data-datepicker-type="datetime"].datetime')
end
end

0 comments on commit c8e03fa

Please sign in to comment.