Class: TaxonWorks::AutoselectGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- TaxonWorks::AutoselectGenerator
- Defined in:
- lib/generators/taxon_works/autoselect/autoselect_generator.rb
Overview
Usage:
rails generate taxon_works:autoselect <model_name> [level_name ...] [--fast] [--no-example]
Examples:
rails generate taxon_works:autoselect taxon_name --fast catalog_of_life
rails generate taxon_works:autoselect otu catalog_of_life
Instance Method Summary collapse
- #create_autoselect ⇒ Object
- #create_custom_levels ⇒ Object
- #create_fast_level ⇒ Object
- #create_operators_stub ⇒ Object
-
#custom_level_names ⇒ Object
private
Custom (non-built-in) level names from arguments, excluding operator specs (contain ':').
- #inject_controller_action ⇒ Object
- #inject_route ⇒ Object
-
#level_class_names ⇒ Object
private
Ordered list of level class names for the generated autoselect.rb.
- #model_class_name ⇒ Object private
- #update_debugging_task ⇒ Object
-
#validate_arguments ⇒ Object
Methods execute in declaration order (Thor convention).
Instance Method Details
#create_autoselect ⇒ Object
39 40 41 42 |
# File 'lib/generators/taxon_works/autoselect/autoselect_generator.rb', line 39 def create_autoselect template 'autoselect.rb.tt', "lib/autoselect/#{model_name}/autoselect.rb" end |
#create_custom_levels ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/generators/taxon_works/autoselect/autoselect_generator.rb', line 55 def create_custom_levels custom_level_names.each do |custom_level_name| @current_level_name = custom_level_name template 'levels/custom.rb.tt', "lib/autoselect/#{model_name}/levels/#{custom_level_name}.rb" end end |
#create_fast_level ⇒ Object
49 50 51 52 53 |
# File 'lib/generators/taxon_works/autoselect/autoselect_generator.rb', line 49 def create_fast_level return unless [:fast] template 'levels/fast.rb.tt', "lib/autoselect/#{model_name}/levels/fast.rb" end |
#create_operators_stub ⇒ Object
44 45 46 47 |
# File 'lib/generators/taxon_works/autoselect/autoselect_generator.rb', line 44 def create_operators_stub template 'operators.rb.tt', "lib/autoselect/#{model_name}/operators.rb" end |
#custom_level_names ⇒ Object (private)
Custom (non-built-in) level names from arguments, excluding operator specs (contain ':')
131 132 133 |
# File 'lib/generators/taxon_works/autoselect/autoselect_generator.rb', line 131 def custom_level_names level_names.reject { |l| l.include?(':') } end |
#inject_controller_action ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/generators/taxon_works/autoselect/autoselect_generator.rb', line 75 def inject_controller_action controller_file = "app/controllers/#{model_name.pluralize}_controller.rb" return unless File.exist?(File.join(destination_root, controller_file)) action_code = <<~RUBY # GET /#{model_name.pluralize}/autoselect def autoselect render json: ::Autoselect::#{model_class_name}::Autoselect.new( term: params[:term], level: params[:level], project_id: sessions_current_project_id, user_id: sessions_current_user_id, **autoselect_params ).response end RUBY inject_into_file controller_file, action_code, before: /^ private\b/ params_code = <<~RUBY def autoselect_params params.permit( # TODO: add model-specific permitted autoselect params here ).to_h.symbolize_keys end RUBY inject_into_file controller_file, params_code, after: /^ private\n/ end |
#inject_route ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/generators/taxon_works/autoselect/autoselect_generator.rb', line 63 def inject_route route_file = 'config/routes/data.rb' return unless File.exist?(File.join(destination_root, route_file)) route_line = " get :autoselect, defaults: { format: :json }\n" # Find the collection do block within the model's resources block and inject after it opens. # Regex anchored to the specific model to avoid matching other resources blocks. inject_into_file route_file, route_line, after: /resources :#{model_name.pluralize} do.*?collection do\n/m end |
#level_class_names ⇒ Object (private)
Ordered list of level class names for the generated autoselect.rb
136 137 138 139 140 141 142 |
# File 'lib/generators/taxon_works/autoselect/autoselect_generator.rb', line 136 def level_class_names names = [] names << 'Fast' if [:fast] names << 'Smart' custom_level_names.each { |l| names << l.camelize } names end |
#model_class_name ⇒ Object (private)
126 127 128 |
# File 'lib/generators/taxon_works/autoselect/autoselect_generator.rb', line 126 def model_class_name model_name.camelize end |
#update_debugging_task ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/generators/taxon_works/autoselect/autoselect_generator.rb', line 111 def update_debugging_task return if [:no_example] app_vue = 'app/javascript/vue/tasks/debugging/autoselects/app.vue' return unless File.exist?(app_vue) comment_line = " // { url: '/#{model_name.pluralize}/autoselect', param: '#{model_name}_id', label: '#{model_class_name}' }," inject_into_file app_vue, "\n#{comment_line}", after: 'const registeredModels = ref([' end |
#validate_arguments ⇒ Object
Methods execute in declaration order (Thor convention)
32 33 34 35 36 37 |
# File 'lib/generators/taxon_works/autoselect/autoselect_generator.rb', line 32 def validate_arguments unless model_name.match?(/\A[a-z][a-z_]*\z/) puts Rainbow("ERROR: model_name '#{model_name}' must be snake_case (lowercase letters and underscores only)").red abort end end |