From 977d2b24e9f25f090a6dac7295434b91eec5cb3e Mon Sep 17 00:00:00 2001 From: Jurgens du Toit Date: Fri, 20 Oct 2023 12:52:01 +0200 Subject: [PATCH] chore: Generator and form control tweaks --- lib/ditty/components/ditty.rb | 1 - lib/ditty/generators/crud_generator.rb | 2 -- lib/ditty/helpers/views.rb | 11 +++++++---- lib/ditty/templates/model.rb.erb | 11 +++++++++++ views/partials/filter_control.haml | 13 ++++++++----- 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/lib/ditty/components/ditty.rb b/lib/ditty/components/ditty.rb index f78d4b9..67a9e65 100644 --- a/lib/ditty/components/ditty.rb +++ b/lib/ditty/components/ditty.rb @@ -23,7 +23,6 @@ def self.configure(_container) require 'ditty/db' unless defined? ::DB require 'ditty/listener' - self.load end def self.migrations diff --git a/lib/ditty/generators/crud_generator.rb b/lib/ditty/generators/crud_generator.rb index a6e8b9c..7feb900 100644 --- a/lib/ditty/generators/crud_generator.rb +++ b/lib/ditty/generators/crud_generator.rb @@ -38,8 +38,6 @@ def create_model def create_controller filename = File.join("lib/#{folder}/controllers", "#{controller_name.underscore}.rb") template '../templates/controller.rb.erb', filename - # TODO: Insert the route into the component file - # insert_into_file 'config.ru', "use #{class_name}\n", after: 'run ApplicationController\n' rescue StandardError => e puts "Could not generate controller for #{model_name}: #{e.message}" end diff --git a/lib/ditty/helpers/views.rb b/lib/ditty/helpers/views.rb index 9e0c1eb..f4f44ad 100644 --- a/lib/ditty/helpers/views.rb +++ b/lib/ditty/helpers/views.rb @@ -45,14 +45,17 @@ def form_control(name, model, opts = {}) end def filter_control(filter, opts = {}) - meth = "#{filter[:name]}_options".to_sym - return unless respond_to? meth + options = if opts[:options] + opts[:options] + else + meth = "#{filter[:name]}_options".to_sym + send(meth) if respond_to? meth + end haml :'partials/filter_control', locals: { name: filter[:name], label: opts[:label] || filter[:name].to_s.titlecase, - options: send(meth), - total_filters: opts[:filters] + options: options } end diff --git a/lib/ditty/templates/model.rb.erb b/lib/ditty/templates/model.rb.erb index b1e0a7d..a00a3b1 100644 --- a/lib/ditty/templates/model.rb.erb +++ b/lib/ditty/templates/model.rb.erb @@ -9,6 +9,10 @@ module <%= namespace %> class <%= model_name %> < ::Sequel::Model include ::Ditty::Base + <%- if columns.include? :guid -%> + plugin :uuid, field: :guid + <%- end -%> + <%- many_to_ones.each do |key| -%> many_to_one :<%= key[:table].to_s.singularize %> <%- end -%> @@ -24,3 +28,10 @@ module <%= namespace %> end end end + +# Simple columns, use `bundle exec rake sequel:annotate` for more +# Table: <%= "#{namespace}::#{model_name}".constantize.table_name %> +# Columns: +<%- "#{namespace}::#{model_name}".constantize.columns.each do |colm| -%> +# <%= colm %> +<%- end -%> \ No newline at end of file diff --git a/views/partials/filter_control.haml b/views/partials/filter_control.haml index 5cb1ff6..1880be7 100644 --- a/views/partials/filter_control.haml +++ b/views/partials/filter_control.haml @@ -1,6 +1,9 @@ -.form-group{ class: total_filters.to_i > 4 ? 'col-3' : 'col-md'} +.form-group.col %label{ for: "filter_#{name}" }= label - %select.form-control.select2{ style: 'width: 100%', name: name, id: "filter_#{name}" } - %option{ value: '' } -- Select One -- - - options.each do |k,v| k ||= v; v ||= k; - %option{ value: k, selected: (params[name].eql? k)}= v + - if options + %select.form-control.select2{ style: 'width: 100%', name: name, id: "filter_#{name}" } + %option{ value: '' } -- Select One -- + - options.each do |k,v| k ||= v; v ||= k; + %option{ value: k, selected: (params[name].eql? k)}= v + - else + %input.form-control{ name: name, id: "filter_#{name}", value: params[name] } \ No newline at end of file