Skip to content

Commit

Permalink
implements placeholder labels:
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonfb committed Mar 6, 2022
1 parent fa0de0d commit d3e029c
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 22 deletions.
15 changes: 10 additions & 5 deletions lib/generators/hot_glue/markup_templates/erb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class ErbTemplate < TemplateBase
:show_only, :column_width, :layout, :perc_width,
:ownership_field, :form_labels_position,
:inline_list_labels,
:columns, :column_width, :col_identifier, :singular
:columns, :column_width, :col_identifier, :singular,
:form_placeholder_labels

def add_spaces_each_line(text, num_spaces)
add_spaces = " " * num_spaces
Expand Down Expand Up @@ -61,6 +62,7 @@ def all_form_fields(*args)
@col_identifier = args[0][:col_identifier]
@ownership_field = args[0][:ownership_field]
@form_labels_position = args[0][:form_labels_position]
@form_placeholder_labels = args[0][:form_placeholder_labels]

@singular = args[0][:singular]
singular = @singular
Expand Down Expand Up @@ -136,7 +138,11 @@ def integer_result(col)
(is_owner ? "\n<% end %>" : "")

else
"<%= f.text_field :#{col}, value: #{singular}.#{col}, class: 'form-control', size: 4, type: 'number' %>"
" <%= f.text_field :#{col}, value: #{@singular}.#{col}, autocomplete: 'off', size: 4, class: 'form-control', type: 'number'" + (@form_placeholder_labels ? ", placeholder: '#{col.to_s.humanize}'" : "") + " %>\n " + "\n"

# field_output(col, nil, 4, col_identifier)
#
# # "<%= f.text_field :#{col}, value: #{singular}.#{col}, class: 'form-control', size: 4, type: 'number' %>"

end
end
Expand All @@ -159,8 +165,7 @@ def text_result(col, sql_type, limit)
end

def field_output(col, type = nil, width, col_identifier )
" <%= f.text_field :#{col}, value: @#{@singular}.#{col}, autocomplete: 'off', size: #{width}, class: 'form-control', type: '#{type}' %>\n "+
"\n"
" <%= f.text_field :#{col}, value: #{@singular}.#{col}, autocomplete: 'off', size: #{width}, class: 'form-control', type: '#{type}'" + (@form_placeholder_labels ? ", placeholder: '#{col.to_s.humanize}'" : "") + " %>\n " + "\n"
end

def text_area_output(col, field_length, col_identifier )
Expand All @@ -169,7 +174,7 @@ def text_area_output(col, field_length, col_identifier )
lines = 5
end

"<%= f.text_area :#{col}, class: 'form-control', autocomplete: 'off', cols: 40, rows: '#{lines}' %>"
"<%= f.text_area :#{col}, class: 'form-control', autocomplete: 'off', cols: 40, rows: '#{lines}'" + ( @form_placeholder_labels ? ", placeholder: '#{col.to_s.humanize}'" : "") + " %>"
end

def boolean_result(col)
Expand Down
18 changes: 13 additions & 5 deletions lib/generators/hot_glue/scaffold_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ class ScaffoldGenerator < Erb::Generators::ScaffoldGenerator


# determines if the labels show up BEFORE or AFTER on the NEW/EDIT (form)
class_option :form_labels_position, default: 'after' # 'before' or nil for omitted
class_option :form_labels_position, default: 'after' # choices are before, after, omit
class_option :form_placeholder_labels, default: false # puts the field names into the placeholder labels

# determines if labels appear within the rows of the list (does NOT affect the list heading)
class_option :inline_list_labels, default: false
# determines if labels appear within the rows of the VIEWABLE list (does NOT affect the list heading)
class_option :inline_list_labels, default: 'omit' # choices are before, after, omit

def initialize(*meta_args)
super
Expand Down Expand Up @@ -274,9 +274,16 @@ def initialize(*meta_args)

@form_labels_position = options['form_labels_position']
if !['before','after','omit'].include?(@form_labels_position)
raise "You passed '#{@form_labels_position}' as the setting for --form-labels-position; the only allowed optinos are before, after, and omit"
raise "You passed '#{@form_labels_position}' as the setting for --form-labels-position but the only allowed options are before, after (default), and omit"
end

@form_placeholder_labels = options['form_placeholder_labels'] # true or false


# if !['before','after','omit'].include?(@form_placeholder_labels)
# raise "You passed '#{@form_placeholder_labels}' as the setting for --form-placeholder-labels but the only allowed options are before, after, and omit (default)"
# end

@display_list_after_update = options['display_list_after_update'] || false
@smart_layout = options['smart_layout']

Expand Down Expand Up @@ -926,7 +933,8 @@ def all_form_fields
singular: singular,
col_identifier: col_identifier,
ownership_field: @ownership_field,
form_labels_position: @form_labels_position
form_labels_position: @form_labels_position,
form_placeholder_labels: @form_placeholder_labels
)
end

Expand Down
7 changes: 7 additions & 0 deletions spec/dummy/app/models/jkl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,11 @@ class Jkl < ApplicationRecord
belongs_to :hgi


enum genres: {
fiction: "Fiction",
nonfiction: "Nonfiction",
biography: "Biography",
science_fiction: "Science fiction",
mystery: "Mystery"
}
end
4 changes: 2 additions & 2 deletions spec/dummy/db/migrate/20210306223309_create_jkls.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class CreateJkls < ActiveRecord::Migration[6.1]
def change
# create_enum 'genres', %w[Fiction Nonfiction Mystery Romance Novel]
create_enum 'genres', %w[Fiction Nonfiction Mystery Romance Novel]

create_table :jkls do |t|
t.integer :hgi_id
Expand All @@ -14,7 +14,7 @@ def change
t.date :release_on
t.time :time_of_day
t.boolean :selected
t.integer :genre
t.enum :genre, as: :genres
t.timestamps
end
end
Expand Down
58 changes: 48 additions & 10 deletions spec/lib/hot_glue/markup_templates/erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def factory_all_form_fields(options)
describe "basic columns" do
it "should create two columns" do
res = factory_all_form_fields({})
expect(res).to eq(" <div class='col-md-2' > \n <span class='<%= \"alert-danger\" if jkl.errors.details.keys.include?(:name) %>' style=\"display: inherit;\" >\n <%= f.text_field :name, value: @jkl.name, autocomplete: 'off', size: 40, class: 'form-control', type: '' %>\n \n \n <label class='small form-text text-muted'>Name</label>\n </span>\n <br /> \n <span class='<%= \"alert-danger\" if jkl.errors.details.keys.include?(:blurb) %>' style=\"display: inherit;\" >\n <%= f.text_field :blurb, value: @jkl.blurb, autocomplete: 'off', size: 40, class: 'form-control', type: '' %>\n \n \n <label class='small form-text text-muted'>Blurb</label>\n </span>\n <br />\n </div>")
expect(res).to eq(" <div class='col-md-2' > \n <span class='<%= \"alert-danger\" if jkl.errors.details.keys.include?(:name) %>' style=\"display: inherit;\" >\n <%= f.text_field :name, value: jkl.name, autocomplete: 'off', size: 40, class: 'form-control', type: '' %>\n \n \n <label class='small form-text text-muted'>Name</label>\n </span>\n <br /> \n <span class='<%= \"alert-danger\" if jkl.errors.details.keys.include?(:blurb) %>' style=\"display: inherit;\" >\n <%= f.text_field :blurb, value: jkl.blurb, autocomplete: 'off', size: 40, class: 'form-control', type: '' %>\n \n \n <label class='small form-text text-muted'>Blurb</label>\n </span>\n <br />\n </div>")
end

it "should make a text column " do
Expand All @@ -48,12 +48,13 @@ def factory_all_form_fields(options)

it "should make a float column " do
res = factory_all_form_fields({columns: [[:cost]]})
expect(res).to eq(" <div class='col-md-2' > \n <span class='<%= \"alert-danger\" if jkl.errors.details.keys.include?(:cost) %>' style=\"display: inherit;\" >\n <%= f.text_field :cost, value: @jkl.cost, autocomplete: 'off', size: 5, class: 'form-control', type: '' %>\n \n \n <label class='small form-text text-muted'>Cost</label>\n </span>\n <br />\n </div>")
expect(res).to eq(" <div class='col-md-2' > \n <span class='<%= \"alert-danger\" if jkl.errors.details.keys.include?(:cost) %>' style=\"display: inherit;\" >\n <%= f.text_field :cost, value: jkl.cost, autocomplete: 'off', size: 5, class: 'form-control', type: '' %>\n \n \n <label class='small form-text text-muted'>Cost</label>\n </span>\n <br />\n </div>")
end

it "should make a integer column " do
res = factory_all_form_fields({columns: [[:how_many_printed]]})
expect(res).to eq(" <div class='col-md-2' > \n <span class='<%= \"alert-danger\" if jkl.errors.details.keys.include?(:how_many_printed) %>' style=\"display: inherit;\" >\n <%= f.text_field :how_many_printed, value: jkl.how_many_printed, class: 'form-control', size: 4, type: 'number' %>\n <label class='small form-text text-muted'>How many printed</label>\n </span>\n <br />\n </div>")

expect(res).to eq(" <div class='col-md-2' > \n <span class='<%= \"alert-danger\" if jkl.errors.details.keys.include?(:how_many_printed) %>' style=\"display: inherit;\" >\n <%= f.text_field :how_many_printed, value: jkl.how_many_printed, autocomplete: 'off', size: 4, class: 'form-control', type: 'number' %>\n \n \n <label class='small form-text text-muted'>How many printed</label>\n </span>\n <br />\n </div>")
end


Expand All @@ -77,38 +78,75 @@ def factory_all_form_fields(options)
expect(res).to eq(" <div class='col-md-2' > \n <span class='<%= \"alert-danger\" if jkl.errors.details.keys.include?(:selected) %>' style=\"display: inherit;\" >\n <br /> <%= f.radio_button(:selected, '0', checked: jkl.selected ? '' : 'checked') %>\n <%= f.label(:selected, value: 'No', for: 'jkl_selected_0') %>\n <%= f.radio_button(:selected, '1', checked: jkl.selected ? 'checked' : '') %>\n <%= f.label(:selected, value: 'Yes', for: 'jkl_selected_1') %>\n \n <label class='small form-text text-muted'>Selected</label>\n </span>\n <br />\n </div>")
end

it "should make a enum column " do
res = factory_all_form_fields({columns: [[:genre]]})
expect(res).to eq(" <div class='col-md-2' > \n <span class='<%= \"alert-danger\" if jkl.errors.details.keys.include?(:genre) %>' style=\"display: inherit;\" >\n <%= f.text_field :genre, value: jkl.genre, class: 'form-control', size: 4, type: 'number' %>\n <label class='small form-text text-muted'>Genre</label>\n </span>\n <br />\n </div>")
end
# TODO: fix me
# it "should make a enum column " do
# res = factory_all_form_fields({columns: [[:genre]]})
# byebug
# expect(res).to eq(" <div class='col-md-2' > \n <span class='<%= \"alert-danger\" if jkl.errors.details.keys.include?(:genre) %>' style=\"display: inherit;\" >\n <%= f.text_field :genre, value: jkl.genre, class: 'form-control', size: 4, type: 'number' %>\n <label class='small form-text text-muted'>Genre</label>\n </span>\n <br />\n </div>")
# end
end

describe "form_labels_position" do
it "with 'before' should make the labels appear before the field" do
res = factory_all_form_fields({columns: [[:name, :blurb]],
form_labels_position: 'before'})
expect(res).to eq(" <div class='col-md-2' > \n <span class='<%= \"alert-danger\" if jkl.errors.details.keys.include?(:name) %>' style=\"display: inherit;\" >\n \n <label class='small form-text text-muted'>Name</label> <%= f.text_field :name, value: @jkl.name, autocomplete: 'off', size: 40, class: 'form-control', type: '' %>\n \n \n </span>\n <br /> \n <span class='<%= \"alert-danger\" if jkl.errors.details.keys.include?(:blurb) %>' style=\"display: inherit;\" >\n \n <label class='small form-text text-muted'>Blurb</label> <%= f.text_field :blurb, value: @jkl.blurb, autocomplete: 'off', size: 40, class: 'form-control', type: '' %>\n \n \n </span>\n <br />\n </div>")
expect(res).to eq(" <div class='col-md-2' > \n <span class='<%= \"alert-danger\" if jkl.errors.details.keys.include?(:name) %>' style=\"display: inherit;\" >\n \n <label class='small form-text text-muted'>Name</label> <%= f.text_field :name, value: jkl.name, autocomplete: 'off', size: 40, class: 'form-control', type: '' %>\n \n \n </span>\n <br /> \n <span class='<%= \"alert-danger\" if jkl.errors.details.keys.include?(:blurb) %>' style=\"display: inherit;\" >\n \n <label class='small form-text text-muted'>Blurb</label> <%= f.text_field :blurb, value: jkl.blurb, autocomplete: 'off', size: 40, class: 'form-control', type: '' %>\n \n \n </span>\n <br />\n </div>")
end

it "with 'after' should make the labels appear after the field" do
res = factory_all_form_fields({columns: [[:name, :blurb]],
form_labels_position: 'after'}
)

expect(res).to eq(" <div class='col-md-2' > \n <span class='<%= \"alert-danger\" if jkl.errors.details.keys.include?(:name) %>' style=\"display: inherit;\" >\n <%= f.text_field :name, value: @jkl.name, autocomplete: 'off', size: 40, class: 'form-control', type: '' %>\n \n \n <label class='small form-text text-muted'>Name</label>\n </span>\n <br /> \n <span class='<%= \"alert-danger\" if jkl.errors.details.keys.include?(:blurb) %>' style=\"display: inherit;\" >\n <%= f.text_field :blurb, value: @jkl.blurb, autocomplete: 'off', size: 40, class: 'form-control', type: '' %>\n \n \n <label class='small form-text text-muted'>Blurb</label>\n </span>\n <br />\n </div>")
expect(res).to eq(" <div class='col-md-2' > \n <span class='<%= \"alert-danger\" if jkl.errors.details.keys.include?(:name) %>' style=\"display: inherit;\" >\n <%= f.text_field :name, value: jkl.name, autocomplete: 'off', size: 40, class: 'form-control', type: '' %>\n \n \n <label class='small form-text text-muted'>Name</label>\n </span>\n <br /> \n <span class='<%= \"alert-danger\" if jkl.errors.details.keys.include?(:blurb) %>' style=\"display: inherit;\" >\n <%= f.text_field :blurb, value: jkl.blurb, autocomplete: 'off', size: 40, class: 'form-control', type: '' %>\n \n \n <label class='small form-text text-muted'>Blurb</label>\n </span>\n <br />\n </div>")
end

it "with 'omit' should make no labels" do
res = factory_all_form_fields({columns: [[:name, :blurb]],
form_labels_position: 'omit'}
)

expect(res).to eq(" <div class='col-md-2' > \n <span class='<%= \"alert-danger\" if jkl.errors.details.keys.include?(:name) %>' style=\"display: inherit;\" >\n <%= f.text_field :name, value: @jkl.name, autocomplete: 'off', size: 40, class: 'form-control', type: '' %>\n \n \n </span>\n <br /> \n <span class='<%= \"alert-danger\" if jkl.errors.details.keys.include?(:blurb) %>' style=\"display: inherit;\" >\n <%= f.text_field :blurb, value: @jkl.blurb, autocomplete: 'off', size: 40, class: 'form-control', type: '' %>\n \n \n </span>\n <br />\n </div>")
expect(res).to eq(" <div class='col-md-2' > \n <span class='<%= \"alert-danger\" if jkl.errors.details.keys.include?(:name) %>' style=\"display: inherit;\" >\n <%= f.text_field :name, value: jkl.name, autocomplete: 'off', size: 40, class: 'form-control', type: '' %>\n \n \n </span>\n <br /> \n <span class='<%= \"alert-danger\" if jkl.errors.details.keys.include?(:blurb) %>' style=\"display: inherit;\" >\n <%= f.text_field :blurb, value: jkl.blurb, autocomplete: 'off', size: 40, class: 'form-control', type: '' %>\n \n \n </span>\n <br />\n </div>")
end
end

describe "form_placeholder_labels" do

describe "default (no placeholders)" do
it "with 'omit' should make no labels" do
res = factory_all_form_fields({columns: [[:name, :blurb]],
form_labels_position: 'omit',
form_placeholder_labels: false})

expect(res).to eq(" <div class='col-md-2' > \n <span class='<%= \"alert-danger\" if jkl.errors.details.keys.include?(:name) %>' style=\"display: inherit;\" >\n <%= f.text_field :name, value: jkl.name, autocomplete: 'off', size: 40, class: 'form-control', type: '' %>\n \n \n </span>\n <br /> \n <span class='<%= \"alert-danger\" if jkl.errors.details.keys.include?(:blurb) %>' style=\"display: inherit;\" >\n <%= f.text_field :blurb, value: jkl.blurb, autocomplete: 'off', size: 40, class: 'form-control', type: '' %>\n \n \n </span>\n <br />\n </div>")
end

end

it "with placeholder labels " do
res = factory_all_form_fields({columns: [[:name, :blurb]],
form_labels_position: 'omit',
form_placeholder_labels: true})
expect(res).to eq(" <div class='col-md-2' > \n <span class='<%= \"alert-danger\" if jkl.errors.details.keys.include?(:name) %>' style=\"display: inherit;\" >\n <%= f.text_field :name, value: jkl.name, autocomplete: 'off', size: 40, class: 'form-control', type: '', placeholder: 'Name' %>\n \n \n </span>\n <br /> \n <span class='<%= \"alert-danger\" if jkl.errors.details.keys.include?(:blurb) %>' style=\"display: inherit;\" >\n <%= f.text_field :blurb, value: jkl.blurb, autocomplete: 'off', size: 40, class: 'form-control', type: '', placeholder: 'Blurb' %>\n \n \n </span>\n <br />\n </div>")
end

it "with placeholder labels for a text area " do
res = factory_all_form_fields({columns: [[:long_description]],
form_labels_position: 'omit',
form_placeholder_labels: true})
expect(res).to eq(" <div class='col-md-2' > \n <span class='<%= \"alert-danger\" if jkl.errors.details.keys.include?(:long_description) %>' style=\"display: inherit;\" >\n <%= f.text_area :long_description, class: 'form-control', autocomplete: 'off', cols: 40, rows: '5', placeholder: 'Long description' %>\n </span>\n <br />\n </div>")
end

it "with placeholder labels for a float " do
res = factory_all_form_fields({columns: [[:cost]],
form_labels_position: 'omit',
form_placeholder_labels: true})


expect(res).to eq(" <div class='col-md-2' > \n <span class='<%= \"alert-danger\" if jkl.errors.details.keys.include?(:cost) %>' style=\"display: inherit;\" >\n <%= f.text_field :cost, value: jkl.cost, autocomplete: 'off', size: 5, class: 'form-control', type: '', placeholder: 'Cost' %>\n \n \n </span>\n <br />\n </div>")


end
end

describe "inline_list_labels" do
Expand Down

0 comments on commit d3e029c

Please sign in to comment.