Skip to content

Commit

Permalink
fixes modify_date_inputs_on_params #current_user_object (#180)
Browse files Browse the repository at this point in the history
* fixes modify_date_inputs_on_params #current_user_object

* Adds back alt lookup feature from 0.5.7

* Fixes specs

* Readme

* v0.6.8

* readme

* Gemfile.lock
  • Loading branch information
jasonfb authored Dec 4, 2024
1 parent f3b50a5 commit c7fac0d
Show file tree
Hide file tree
Showing 12 changed files with 208 additions and 50 deletions.
8 changes: 4 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
hot-glue (0.6.7)
hot-glue (0.6.8)
ffaker (~> 2.16)
kaminari (~> 1.2)
rails (> 5.1)
Expand Down Expand Up @@ -90,7 +90,7 @@ GEM
xpath (~> 3.2)
concurrent-ruby (1.1.10)
crass (1.0.6)
date (3.3.4)
date (3.4.1)
devise (4.8.1)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
Expand Down Expand Up @@ -139,7 +139,7 @@ GEM
mini_mime (1.1.2)
mini_portile2 (2.8.4)
minitest (5.16.3)
net-imap (0.4.14)
net-imap (0.5.1)
date
net-protocol
net-pop (0.1.2)
Expand Down Expand Up @@ -235,7 +235,7 @@ GEM
stimulus-rails (1.1.1)
railties (>= 6.0.0)
thor (1.2.1)
timeout (0.4.1)
timeout (0.4.2)
turbo-rails (1.3.2)
actionpack (>= 6.0.0)
activejob (>= 6.0.0)
Expand Down
66 changes: 64 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -690,10 +690,60 @@ Notice that each modifiers can be used with specific field types.
| tinymce | applies to text fields only, be sure to setup TineMCE globally | text fields only | | |
| typeahead | turns a foreign key (only) into a searchable typeahead field | foreign keys only | | |
| timezone | turns a string (varchar) into a drop down of timezones | foreign keys only | | |

| none | special modifier for using badges |

Except for "(truthy label)" and "(falsy label)" which use the special syntax, use the modifier _exactly_ as it is named.

apply badge behavior using `[` and `]` markers after the modification marker.

`--modify=opened_at{opened|closed}[bg-primary|bg-secondary]`
Applies a badge `bg-primary` to rows with opened_at truthy and `bg-secondary` to rows with opened_at falsy.

to display a badge on everything, use the `none` modifier with the
`--modify=opened_at{none}[bg-dark]`



### `--alt-foreign-key-lookup=`

Use for a join table to specify that a field should be looked up by a different field


`./bin/rails generate hot_glue:scaffold AccountUser --alt-foreign-key-lookup=user_id{email}`

Here we are specifying that the `user_id` field should be looked up by the `email` field on the User table.
If no existing user exists, we create one because we are using the `find_or_create_by!` method.

Use with a factory pattern like this one:
```
class AccountUserFactory
attr_accessor :account_user
def initialize(params: {}, account: nil)
begin
user = User.find_or_create_by!( email: params[:__lookup_email])
rescue ActiveRecord::RecordInvalid => e
@account_user = AccountUser.new({account: account})
@account_user.errors.add(:user, e.message)
end
@account_user = AccountUser.new(params.tap{|x| x.delete(:__lookup_email)}
.merge({user: user,
account: account}))
end
end
```

this works with a factory creation syntax like so:

```
--factory-creation='factory = AccountUserFactory.new(params: account_user_params, account: account)
```
*See the `--factory-creation` section.



### `--pundit`
If you enable Pundit, your controllers will look for a Policy that matches the name of the thing being built.

Expand Down Expand Up @@ -1612,6 +1662,18 @@ These automatic pickups for partials are detected at buildtime. This means that

# VERSION HISTORY


#### 2024-12-05 - v0.6.8
• fixes in modify_date_inputs_on_params for current_user_object

• adds back alt_lookup feature from version 0.5.7; use with --alt-foreign-key-lookup

• badges can be added to modified fields using `[` and `]` which come after the modification flag inside `{...}`.
for booleans separate with pipes `|`

• you can add badges to fields that have no other modification using the `none` modifier


#### 2024-11-26 - v0.6.7

Patch for my non-:00 seconds problem. I have discovered that the root of my issues was a quirk in how browsers display datetime-local fields.
Expand Down Expand Up @@ -2154,7 +2216,7 @@ div[class$="--phone_number"] {
#### 2023-02-13 - v0.5.7 - factory-creation, alt lookups, update show only, fixes to Enums, support for Ruby 3.2
• See `--factory-creation` section.

`--alt-lookup-foreign-keys`
`--alt-foreign-key-lookup`
Allows you to specify that a foreign key should act as a search field, allowing the user to input a unique value (like an email) to search for a related record.

`--update-show-only`
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/hot_glue/controller_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def date_to_current_timezone(date, timezone = nil)
end

def modify_date_inputs_on_params(modified_params, current_user_object = nil, field_list = nil)
use_timezone = (current_user.try(:timezone)) || Time.zone
use_timezone = (current_user_object.try(:timezone)) || Time.zone
modified_params = modified_params.tap do |params|
params.keys.each{|k|

Expand Down
2 changes: 2 additions & 0 deletions lib/generators/hot_glue/field_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def initialize(type: , name: , generator: )
end
@class_name = class_name


@field = field_class.new(name: name,
layout_strategy: generator.layout_strategy,
form_placeholder_labels: generator.form_placeholder_labels,
Expand All @@ -57,6 +58,7 @@ def initialize(type: , name: , generator: )
hawk_keys: generator.hawk_keys,
auth: generator.auth,
class_name: generator.singular_class,
alt_lookup: generator.alt_lookups[name] || nil,
singular: generator.singular,
self_auth: generator.self_auth,
update_show_only: generator.update_show_only,
Expand Down
61 changes: 42 additions & 19 deletions lib/generators/hot_glue/fields/association_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@

class AssociationField < Field

attr_accessor :assoc_name, :assoc_class, :assoc
attr_accessor :assoc_name, :assoc_class, :assoc, :alt_lookup

def initialize( class_name: , default_boolean_display:, display_as: ,
def initialize( alt_lookup: , class_name: , default_boolean_display:, display_as: ,
name: , singular: ,
update_show_only: ,
hawk_keys: , auth: , sample_file_path:, ownership_field: ,
attachment_data: nil , layout_strategy: , form_placeholder_labels: nil,
form_labels_position:, modify_as: , self_auth: , namespace:, pundit: )
super


@assoc_model = eval("#{class_name}.reflect_on_association(:#{assoc})")

if assoc_model.nil?
Expand Down Expand Up @@ -47,7 +48,7 @@ def assoc
end

def spec_setup_and_change_act(which_partial)
if which_partial == :update && update_show_only.include?(name)
if which_partial == :update_show_only && update_show_only.include?(name)

else
" #{name}_selector = find(\"[name='#{singular}[#{name}]']\").click \n" +
Expand Down Expand Up @@ -75,10 +76,38 @@ def spec_related_column_lets
" let!(:#{assoc}1) {create(:#{the_foreign_class}" + hawk_keys_on_lets + ")}"
end

def raw_view_field
assoc_name = name.to_s.gsub("_id","")
assoc = eval("#{class_name}.reflect_on_association(:#{assoc_name})")
if assoc.nil?
exit_message = "*** Oops. on the #{singular_class} object, there doesn't seem to be an association called '#{assoc_name}'"
exit
end
is_owner = name == ownership_field
assoc_class_name = assoc.class_name.to_s
display_column = HotGlue.derrive_reference_name(assoc_class_name)


"<%= #{singular}.#{assoc_name}.#{display_column} %>"
end

def form_field_output
assoc_name = name.to_s.gsub("_id","")
assoc = eval("#{class_name}.reflect_on_association(:#{assoc_name})")
if modify_as && modify_as[:typeahead]

if alt_lookup
alt = alt_lookup[:lookup_as]
assoc_name = name.to_s.gsub("_id","")
assoc = eval("#{class_name}.reflect_on_association(:#{assoc_name})")

alt = alt_lookup[:lookup_as]
"<%= f.text_field :__lookup_#{alt}, value: @#{singular}.#{assoc_name}.try(:#{alt}), placeholder: \"search by #{alt}\" %>"

# if modify_as
# modified_display_output
# else
# end
elsif modify_as && modify_as[:typeahead]
search_url = "#{namespace ? namespace + "_" : ""}#{assoc.class_name.downcase.pluralize}_typeahead_index_url"
"<div class='typeahead typeahead--#{assoc.name}_id'
data-controller='typeahead'
Expand Down Expand Up @@ -122,25 +151,19 @@ def field_error_name
end

def form_show_only_output
assoc_name = name.to_s.gsub("_id","")
assoc = eval("#{class_name}.reflect_on_association(:#{assoc_name})")
if assoc.nil?
exit_message = "*** Oops. on the #{singular_class} object, there doesn't seem to be an association called '#{assoc_name}'"
exit
end

is_owner = name == ownership_field
assoc_class_name = assoc.class_name.to_s
display_column = HotGlue.derrive_reference_name(assoc_class_name)

if hawk_keys[assoc.foreign_key.to_sym]
hawk_definition = hawk_keys[assoc.foreign_key.to_sym]
hawked_association = hawk_definition.join(".")
# if hawk_keys[assoc.foreign_key.to_sym]
# hawk_definition = hawk_keys[assoc.foreign_key.to_sym]
# hawked_association = hawk_definition.join(".")
# else
# hawked_association = "#{assoc.class_name}.all"
# end
if modify_as && modify_as[:none]
"<span class='badge #{modify_as[:badges]}'>" + raw_view_field + "</span>"
else
hawked_association = "#{assoc.class_name}.all"
raw_view_field
end
"<%= #{singular}.#{assoc_name}.#{display_column} %>"

end

def line_field_output
Expand Down
23 changes: 19 additions & 4 deletions lib/generators/hot_glue/fields/attachment_field.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
class AttachmentField < Field
attr_accessor :attachment_data
def initialize(name:, class_name:, default_boolean_display: ,
display_as:, singular:, update_show_only:, hawk_keys:, auth:,
sample_file_path: nil, attachment_data:, ownership_field:, layout_strategy: ,
form_placeholder_labels: , form_labels_position:, modify_as:, self_auth: , namespace:, pundit: )
def initialize(alt_lookup:,
attachment_data:,
auth:,
class_name:,
display_as:, singular:,
default_boolean_display: ,
form_placeholder_labels: ,
form_labels_position:,
hawk_keys:,
layout_strategy: ,
name:,
namespace:,
modify_as:,
ownership_field:,
pundit: ,
sample_file_path: nil,
self_auth:,
update_show_only:
)
super

@attachment_data = attachment_data
Expand Down
40 changes: 26 additions & 14 deletions lib/generators/hot_glue/fields/field.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Field
attr_accessor :assoc_model, :assoc_name, :assoc_class, :associations, :alt_lookups, :auth,
attr_accessor :assoc_model, :assoc_name, :assoc_class, :associations, :alt_lookup, :auth,
:assoc_label, :class_name, :default_boolean_display, :display_as, :form_placeholder_labels,
:form_labels_position,
:hawk_keys, :layout_strategy, :limit, :modify_as, :name, :object, :sample_file_path,
Expand All @@ -11,6 +11,7 @@ def initialize(
auth: ,
attachment_data: nil,
class_name: ,
alt_lookup: ,
default_boolean_display: ,
display_as: ,
form_labels_position:,
Expand All @@ -29,7 +30,7 @@ def initialize(
)
@name = name
@layout_strategy = layout_strategy
@alt_lookups = alt_lookups
@alt_lookup = alt_lookup
@singular = singular
@class_name = class_name
@update_show_only = update_show_only
Expand Down Expand Up @@ -123,13 +124,22 @@ def form_show_only_output

def viewable_output
if modify_as
modified_display_output
modified_display_output(show_only: true)
else
field_view_output
end
end

def field_view_output
if modify_as && modify_as[:none]
"<span class='badge #{modify_as[:badges]}'>" + field_view_output + "</span>"
else
"<%= #{singular}.#{name} %>"
end
end

def modified_display_output

def modified_display_output(show_only: false)
res = +''

if modify_as[:cast] && modify_as[:cast] == "$"
Expand All @@ -141,19 +151,21 @@ def modified_display_output
elsif modify_as[:timezone]
res += "<%= #{singular}.#{name} %>"
elsif modify_as[:enum]
res += "<%= render partial: #{singular}.#{name}, locals: {#{singular}: #{singular}} %>"
elsif modify_as[:none]
field_view_output
# res += "<%= render partial: #{singular}.#{name}, locals: {#{singular}: #{singular}} %>"
end

if modify_as[:badges]
badge_code = if modify_as[:binary]
"#{singular}.#{name} ? '#{modify_as[:badges].split("|")[0]}' : '#{modify_as[:badges].split("|")[1]}'"
else
modify_as[:badges].split("|").to_s + "[#{singular}.#{name}]"
end
res = "<span class='badge <%= #{badge_code} %>'>" + res + "</span>"
end
# byebug

# if modify_as[:badges]
# badge_code = if modify_as[:binary]
# "#{singular}.#{name} ? '#{modify_as[:badges].split("|")[0]}' : '#{modify_as[:badges].split("|")[1]}'"
# else
# modify_as[:badges].split("|").to_s + "[#{singular}.#{name}]"
# end
# res = "<span class='badge <%= #{badge_code} %>'>" + res + "</span>"
# end
# byebug
res
end

Expand Down
2 changes: 1 addition & 1 deletion lib/generators/hot_glue/markup_templates/erb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def all_form_fields(layout_strategy:)
elsif update_show_only.include?(col) && @pundit && eval("defined? #{singular_class}Policy") && eval("#{singular_class}Policy").instance_methods.include?("#{col}_able?".to_sym)
"<% if @action == 'new' && policy(@#{singular}).#{col}_able? %>" + columns_map[col].form_field_output + "<% else %>" + columns_map[col].form_show_only_output + "<% end %>"

# show only on the update action overrides any pundit policy
# show only on the update action overrides any pundit policy
elsif @pundit && eval("defined? #{singular_class}Policy") && eval("#{singular_class}Policy").instance_methods.include?("#{col}_able?".to_sym)
"<% if policy(@#{singular}).#{col}_able? %>" + columns_map[col].form_field_output + "<% else %>" + columns_map[col].form_show_only_output + "<% end %>"
else
Expand Down
Loading

0 comments on commit c7fac0d

Please sign in to comment.