class Jsonapi::ResourceGenerator
Public Instance Methods
copy_resource_file()
click to toggle source
# File lib/generators/jsonapi/resource_generator.rb, line 19 def copy_resource_file unless model_klass raise "You must define a #{class_name} model before generating the corresponding resource." end generate_controller generate_serializer generate_application_resource unless application_resource_defined? generate_spec_payload if actions?('create', 'update') generate_strong_resource end generate_route generate_tests generate_resource generate_swagger if docs_controller? end
Private Instance Methods
actions()
click to toggle source
# File lib/generators/jsonapi/resource_generator.rb, line 41 def actions @options['actions'] || %w(index show create update destroy) end
actions?(*methods)
click to toggle source
# File lib/generators/jsonapi/resource_generator.rb, line 45 def actions?(*methods) methods.any? { |m| actions.include?(m) } end
api_namespace()
click to toggle source
# File lib/generators/jsonapi/resource_generator.rb, line 174 def api_namespace @api_namespace ||= begin ns = jsonapi_config['namespace'] if ns.blank? ns = prompt \ header: "What is your API namespace?", description: "This will be used as a route prefix, e.g. if you want the route '/books_api/v1/authors' your namespace would be 'books_api'", default: 'api' update_config!('namespace' => ns) end ns end end
application_resource_defined?()
click to toggle source
# File lib/generators/jsonapi/resource_generator.rb, line 68 def application_resource_defined? 'ApplicationResource'.safe_constantize.present? end
docs_controller?()
click to toggle source
# File lib/generators/jsonapi/resource_generator.rb, line 72 def docs_controller? File.exists?('app/controllers/docs_controller.rb') end
generate_application_resource()
click to toggle source
# File lib/generators/jsonapi/resource_generator.rb, line 63 def generate_application_resource to = File.join('app/resources', class_path, "application_resource.rb") template('application_resource.rb.erb', to) end
generate_controller()
click to toggle source
# File lib/generators/jsonapi/resource_generator.rb, line 53 def generate_controller to = File.join('app/controllers', class_path, "#{file_name.pluralize}_controller.rb") template('controller.rb.erb', to) end
generate_resource()
click to toggle source
# File lib/generators/jsonapi/resource_generator.rb, line 151 def generate_resource to = File.join('app/resources', class_path, "#{file_name}_resource.rb") template('resource.rb.erb', to) end
generate_route()
click to toggle source
# File lib/generators/jsonapi/resource_generator.rb, line 105 def generate_route code = " resources :#{type}" code << ", only: [#{actions.map { |a| ":#{a}" }.join(', ')}]" if actions.length < 5 code << "\n" inject_into_file 'config/routes.rb', after: "scope path: '/v1' do\n" do code end end
generate_serializer()
click to toggle source
# File lib/generators/jsonapi/resource_generator.rb, line 58 def generate_serializer to = File.join('app/serializers', class_path, "serializable_#{file_name}.rb") template('serializer.rb.erb', to) end
generate_spec_payload()
click to toggle source
# File lib/generators/jsonapi/resource_generator.rb, line 85 def generate_spec_payload to = File.join('spec/payloads', class_path, "#{file_name}.rb") template('payload.rb.erb', to) end
generate_strong_resource()
click to toggle source
# File lib/generators/jsonapi/resource_generator.rb, line 90 def generate_strong_resource code = " strong_resource :#{file_name} do\n" attributes.each do |a| type = a.type type = :string if type == :text type = :number if [:float, :decimal].include?(type) code << " attribute :#{a.name}, :#{type}\n" end code << " end\n" inject_into_file 'config/initializers/strong_resources.rb', after: "StrongResources.configure do\n" do code end end
generate_swagger()
click to toggle source
# File lib/generators/jsonapi/resource_generator.rb, line 76 def generate_swagger code = " jsonapi_resource '/v1/#{type}'" code << ", only: [#{actions.map { |a| ":#{a}" }.join(', ')}]" if actions.length < 5 code << "\n" inject_into_file 'app/controllers/docs_controller.rb', before: /^end/ do code end end
generate_tests()
click to toggle source
# File lib/generators/jsonapi/resource_generator.rb, line 114 def generate_tests if actions?('index') to = File.join "spec/api/v1/#{file_name.pluralize}", class_path, "index_spec.rb" template('index_request_spec.rb.erb', to) end if actions?('show') to = File.join "spec/api/v1/#{file_name.pluralize}", class_path, "show_spec.rb" template('show_request_spec.rb.erb', to) end if actions?('create') to = File.join "spec/api/v1/#{file_name.pluralize}", class_path, "create_spec.rb" template('create_request_spec.rb.erb', to) end if actions?('update') to = File.join "spec/api/v1/#{file_name.pluralize}", class_path, "update_spec.rb" template('update_request_spec.rb.erb', to) end if actions?('destroy') to = File.join "spec/api/v1/#{file_name.pluralize}", class_path, "destroy_spec.rb" template('destroy_request_spec.rb.erb', to) end end
jsonapi_config()
click to toggle source
# File lib/generators/jsonapi/resource_generator.rb, line 156 def jsonapi_config File.exists?('.jsonapicfg.yml') ? YAML.load_file('.jsonapicfg.yml') : {} end
model_klass()
click to toggle source
# File lib/generators/jsonapi/resource_generator.rb, line 190 def model_klass class_name.safe_constantize end
omit_comments?()
click to toggle source
# File lib/generators/jsonapi/resource_generator.rb, line 49 def omit_comments? @options['omit-comments'] end
prompt(header: nil, description: nil, default: nil)
click to toggle source
# File lib/generators/jsonapi/resource_generator.rb, line 165 def prompt(header: nil, description: nil, default: nil) say(set_color("\n#{header}", :magenta, :bold)) if header say("\n#{description}") if description answer = ask(set_color("\n(default: #{default}):", :magenta, :bold)) answer = default if answer.blank? && default != 'nil' say(set_color("\nGot it!\n", :white, :bold)) answer end
type()
click to toggle source
# File lib/generators/jsonapi/resource_generator.rb, line 194 def type model_klass.name.underscore.pluralize end
update_config!(attrs)
click to toggle source
# File lib/generators/jsonapi/resource_generator.rb, line 160 def update_config!(attrs) config = jsonapi_config.merge(attrs) File.open('.jsonapicfg.yml', 'w') { |f| f.write(config.to_yaml) } end