Skip to content

Commit

Permalink
Fixes to no-list option (#146)
Browse files Browse the repository at this point in the history
* Fixes to no-list option

* refactors load_all_* code into cleaner function; fixes spec
  • Loading branch information
jasonfb authored Dec 21, 2023
1 parent 832d55e commit e7d0fc2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
21 changes: 20 additions & 1 deletion lib/generators/hot_glue/scaffold_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ class HotGlue::ScaffoldGenerator < Erb::Generators::ScaffoldGenerator
class_option :no_edit, type: :boolean, default: false
class_option :no_list, type: :boolean, default: false
class_option :no_controller, type: :boolean, default: false
class_option :no_list, type: :boolean, default: false
class_option :no_paginate, type: :boolean, default: false
class_option :paginate_per_page_selector, type: :boolean, default: false
class_option :big_edit, type: :boolean, default: false
Expand Down Expand Up @@ -295,6 +294,7 @@ def initialize(*meta_args)

@no_edit = options['no_edit'] || false
@no_list = options['no_list'] || false

@no_controller = options['no_controller'] || false
@no_list = options['no_list'] || false
@no_list_label = options['no_list_label'] || false
Expand Down Expand Up @@ -1387,6 +1387,25 @@ def nested_for_assignments_constructor(top_level = true)
end
end


def load_all_code
res = +""
if pundit
res << "@#{ plural_name } = policy_scope(#{ object_scope }).page(params[:page])#{ n_plus_one_includes }#{ ".per(per)" if @paginate_per_page_selector }"
else

if !@self_auth
res << "@#{ plural_name } = #{ object_scope.gsub("@",'') }#{ n_plus_one_includes }.page(params[:page])#{ ".per(per)" if @paginate_per_page_selector }#{ " if params.include?(:#{ @nested_set.last[:singular]}_id)" if @nested_set.any? && @nested_set[0] && @nested_set[0][:optional] }"
elsif @nested_set[0] && @nested_set[0][:optional]
res << "@#{ plural_name } = #{ class_name }.all"
else
res << "@#{ plural_name } = #{ class_name }.where(id: #{ auth_object.gsub("@",'') }.id)#{ n_plus_one_includes }.page(params[:page])#{ ".per(per)" if @paginate_per_page_selector }"
end
end
res
end


private # thor does something fancy like sending the class all of its own methods during some strange run sequence
# does not like public methods
def cc_filename_with_extensions(name, file_format = format)
Expand Down
17 changes: 7 additions & 10 deletions lib/generators/hot_glue/templates/controller.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,10 @@ class <%= controller_class_name %> < <%= controller_descends_from %>
end<% end %>
<% if @paginate_per_page_selector %>def per
params[:per] || 10
end

<% end %>def load_all_<%= plural %><% if @pundit %>
@<%= plural_name %> = policy_scope(<%= object_scope %>).page(params[:page])<%= n_plus_one_includes %><%= ".per(per)" if @paginate_per_page_selector %>
<% else %> <% if !@self_auth %>
@<%= plural_name %> = <%= object_scope.gsub("@",'') %><%= n_plus_one_includes %>.page(params[:page])<%= ".per(per)" if @paginate_per_page_selector %><%= " if params.include?(:#{ @nested_set.last[:singular]}_id)" if @nested_set.any? && @nested_set[0] && @nested_set[0][:optional] %><% if @nested_set[0] && @nested_set[0][:optional] %>
@<%= plural_name %> = <%= class_name %>.all<% end %><% else %>
@<%= plural_name %> = <%= class_name %>.where(id: <%= auth_object.gsub("@",'') %>.id)<%= n_plus_one_includes %>.page(params[:page])<%= ".per(per)" if @paginate_per_page_selector %><% end %>
<% end %>
end<% end %>
<% unless @no_list %>
def load_all_<%= plural %>
<%= load_all_code %>
end

def index
Expand All @@ -87,7 +82,7 @@ class <%= controller_class_name %> < <%= controller_descends_from %>
rescue Pundit::NotAuthorizedError
flash[:alert] = 'You are not authorized to perform this action.'
render 'layouts/error'<% end %>
end
end<% end %>

<% if create_action %> def new<% if @object_owner_sym %>
@<%= singular_name %> = <% if @pundit %>policy_scope(<% end %><%= class_name %><% if @pundit %>)<% end %>.new<% if eval("#{class_name}.reflect_on_association(:#{@object_owner_sym})").class == ActiveRecord::Reflection::BelongsToReflection %>(<%= @object_owner_sym %>: <%= @object_owner_eval %>)<% end %><% elsif @object_owner_optional && any_nested? %>
Expand Down Expand Up @@ -226,9 +221,11 @@ class <%= controller_class_name %> < <%= controller_descends_from %>
params.require(:<%= testing_name %>).permit(<%= ((fields_filtered_for_strong_params - @show_only ) + @magic_buttons.collect{|x| "__#{x}"}).collect{|sym| ":#{sym}"}.join(", ") %><%= ", " + @related_sets.collect{|key, rs| "#{rs[:association_ids_method]}: []"}.join(", ") if @related_sets.any? %>)
end<% if @update_show_only %>

<% unless @no_edit %>
def update_<%=singular_name%>_params
params.require(:<%= testing_name %>).permit(<%= ((fields_filtered_for_strong_params - @update_show_only) + @magic_buttons.collect{|x| "__#{x}"}).collect{|sym| ":#{sym}"}.join(", ") %><%= ", " + @related_sets.collect{|key, rs| "#{rs[:association_ids_method]}: []"}.join(", ") if @related_sets.any? %>)
end<% end %>
<% end %>

def namespace
<% if @namespace %>'<%= @namespace %>/'<% else %><% end %>
Expand Down

0 comments on commit e7d0fc2

Please sign in to comment.