Sha256: a3cb5f076879d4f2865398926f50b4a3a344f7468c4dc523a9139907c6d9f5d5

Contents?: true

Size: 1.51 KB

Versions: 9

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module Phlex
	module Rails
		class Form < Phlex::HTML
			def initialize(model)
				@model = model
			end

			def self.input_field(method_name, type:)
				define_method method_name do |field, value: @model.attributes[field.to_s], **attributes|
					input(
						name: field_name(field),
						type: type,
						value: value,
						**attributes
					)
				end
			end

			def template(&block)
				form action: @url, method: @method do
					authenticity_token_field
					yield_content(&block)
				end
			end

			def authenticity_token_field
				input(
					name: "authenticity_token",
					type: "hidden",
					value: helpers.form_authenticity_token
				)
			end

			def submit(value)
				input(
					name: "commit",
					type: "submit",
					value: value
				)
			end

			def url
				helpers.url_for(@model)
			end

			def field_name(*field)
				helpers.field_name(ActiveModel::Naming.param_key(@model.class), *field)
			end

			input_field :url_field, type: "url"
			input_field :text_field, type: "text"
			input_field :date_field, type: "date"
			input_field :time_field, type: "time"
			input_field :week_field, type: "week"
			input_field :month_field, type: "month"
			input_field :email_field, type: "email"
			input_field :color_field, type: "color"
			input_field :hidden_field, type: "hidden"
			input_field :search_field, type: "search"
			input_field :password_field, type: "password"
			input_field :telephone_field, type: "tel"
			input_field :datetime_local_field, type: "datetime-local"
		end
	end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
phlex-rails-0.10.0 lib/phlex/rails/form.rb
phlex-rails-0.8.1 lib/phlex/rails/form.rb
phlex-rails-0.9.0 lib/phlex/rails/form.rb
phlex-rails-0.8.0 lib/phlex/rails/form.rb
phlex-rails-0.7.1 lib/phlex/rails/form.rb
phlex-rails-0.7.0 lib/phlex/rails/form.rb
phlex-rails-0.6.1 lib/phlex/rails/form.rb
phlex-rails-0.6.0 lib/phlex/rails/form.rb
phlex-rails-0.5.1 lib/phlex/rails/form.rb