|
| 1 | +module Jsonapi |
| 2 | + class ResourceGenerator < ::Rails::Generators::NamedBase |
| 3 | + source_root File.expand_path('../templates', __FILE__) |
| 4 | + |
| 5 | + class_option :'no-controller', type: :boolean, default: false |
| 6 | + class_option :'no-serializer', type: :boolean, default: false |
| 7 | + class_option :'no-payload', type: :boolean, default: false |
| 8 | + class_option :'no-strong-resources', type: :boolean, default: false |
| 9 | + class_option :'no-test', type: :boolean, default: false |
| 10 | + |
| 11 | + desc "This generator creates a resource file at app/resources" |
| 12 | + def copy_resource_file |
| 13 | + unless @options['no-controller'] |
| 14 | + to = File.join('app/controllers', class_path, "#{file_name.pluralize}_controller.rb") |
| 15 | + template('controller.rb.erb', to) |
| 16 | + end |
| 17 | + |
| 18 | + unless @options['no-serializer'] |
| 19 | + to = File.join('app/serializers', class_path, "serializable_#{file_name}.rb") |
| 20 | + template('serializer.rb.erb', to) |
| 21 | + end |
| 22 | + |
| 23 | + unless 'ApplicationResource'.safe_constantize |
| 24 | + to = File.join('app/resources', class_path, "application_resource.rb") |
| 25 | + template('application_resource.rb.erb', to) |
| 26 | + end |
| 27 | + |
| 28 | + unless @options['no-payload'] |
| 29 | + to = File.join('spec/payloads', class_path, "#{file_name}.rb") |
| 30 | + template('payload.rb.erb', to) |
| 31 | + end |
| 32 | + |
| 33 | + unless @options['no-strong-resources'] |
| 34 | + inject_into_file 'config/initializers/strong_resources.rb', after: "StrongResources.configure do\n" do <<-STR |
| 35 | + strong_resource :#{file_name} do |
| 36 | + # Your attributes go here, e.g. |
| 37 | + # attribute :name, :string |
| 38 | + end |
| 39 | +
|
| 40 | + STR |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + unless @options['no-route'] |
| 45 | + inject_into_file 'config/routes.rb', after: "scope '/api' do\n scope '/v1' do\n" do <<-STR |
| 46 | + resources :#{type} |
| 47 | + STR |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + unless @options['no-test'] |
| 52 | + to = File.join "spec/api/v1/#{file_name.pluralize}", |
| 53 | + class_path, |
| 54 | + "index_spec.rb" |
| 55 | + template('index_request_spec.rb.erb', to) |
| 56 | + |
| 57 | + to = File.join "spec/api/v1/#{file_name.pluralize}", |
| 58 | + class_path, |
| 59 | + "show_spec.rb" |
| 60 | + template('show_request_spec.rb.erb', to) |
| 61 | + |
| 62 | + to = File.join "spec/api/v1/#{file_name.pluralize}", |
| 63 | + class_path, |
| 64 | + "create_spec.rb" |
| 65 | + template('create_request_spec.rb.erb', to) |
| 66 | + |
| 67 | + to = File.join "spec/api/v1/#{file_name.pluralize}", |
| 68 | + class_path, |
| 69 | + "update_spec.rb" |
| 70 | + template('update_request_spec.rb.erb', to) |
| 71 | + |
| 72 | + to = File.join "spec/api/v1/#{file_name.pluralize}", |
| 73 | + class_path, |
| 74 | + "destroy_spec.rb" |
| 75 | + template('destroy_request_spec.rb.erb', to) |
| 76 | + end |
| 77 | + |
| 78 | + to = File.join('app/resources', class_path, "#{file_name}_resource.rb") |
| 79 | + template('resource.rb.erb', to) |
| 80 | + end |
| 81 | + |
| 82 | + def do_thing |
| 83 | + puts "DO THING" |
| 84 | + end |
| 85 | + |
| 86 | + private |
| 87 | + |
| 88 | + def model_klass |
| 89 | + class_name.safe_constantize |
| 90 | + end |
| 91 | + |
| 92 | + def type |
| 93 | + model_klass.name.underscore.pluralize |
| 94 | + end |
| 95 | + end |
| 96 | +end |
0 commit comments