class Shaf::Generator::Controller
Public Instance Methods
add_link_to_root()
click to toggle source
# File lib/shaf/generator/controller.rb, line 78 def add_link_to_root file = 'api/serializers/root_serializer.rb' unless File.exist? file puts "Warning: file '#{file}' does not exist. "\ "Skip adding link to the #{plural_name} collection" end added = false content = [] FileTransactions::ChangeFileCommand.execute(file) do File.readlines(file).reverse_each do |line| if match = !added && line.match(/^(\s*)link /) content.unshift link_content(match[1].to_s) added = true end content.unshift(line) end File.open(file, 'w') { |f| f.puts content } puts "Modified: #{file}" end end
call()
click to toggle source
# File lib/shaf/generator/controller.rb, line 10 def call create_controller create_integration_spec if options[:specs] add_link_to_root end
create_controller()
click to toggle source
# File lib/shaf/generator/controller.rb, line 55 def create_controller content = render(template, opts) write_output(target, content) end
create_integration_spec()
click to toggle source
# File lib/shaf/generator/controller.rb, line 60 def create_integration_spec content = render(spec_template, opts) write_output(spec_target, content) end
link_content(indentation = '')
click to toggle source
# File lib/shaf/generator/controller.rb, line 99 def link_content(indentation = '') <<~DOC.split("\n").map { |line| "#{indentation}#{line}" } # Auto generated doc: # Link to the collection of #{plural_name}. # Method: GET # Example: # ``` # curl -H "Accept: application/hal+json" \\ # -H "Authorization: abcdef" \\ # /#{plural_name} #``` link :#{plural_name}, #{plural_name}_uri DOC end
model_class_name()
click to toggle source
# File lib/shaf/generator/controller.rb, line 27 def model_class_name Utils.model_name(name) end
name()
click to toggle source
# File lib/shaf/generator/controller.rb, line 20 def name n = args.first || '' return n unless n.empty? raise Command::ArgumentError, 'Please provide a controller name when using the controller generator!' end
opts()
click to toggle source
# File lib/shaf/generator/controller.rb, line 65 def opts { name: name, plural_name: plural_name, serializer_class_name: "#{model_class_name}Serializer", model_class_name: model_class_name, controller_class_name: "#{pluralized_model_name}Controller", policy_class_name: "#{model_class_name}Policy", policy_file: "policies/#{name}_policy", params: params } end
params()
click to toggle source
# File lib/shaf/generator/controller.rb, line 16 def params args[1..-1].map { |param| param.split(':')} end
plural_name()
click to toggle source
# File lib/shaf/generator/controller.rb, line 31 def plural_name Utils.pluralize(name) end
pluralized_model_name()
click to toggle source
# File lib/shaf/generator/controller.rb, line 35 def pluralized_model_name Utils.pluralize(model_class_name) end
spec_target()
click to toggle source
# File lib/shaf/generator/controller.rb, line 51 def spec_target "spec/integration/#{plural_name}_controller_spec.rb" end
spec_template()
click to toggle source
# File lib/shaf/generator/controller.rb, line 43 def spec_template 'spec/integration_spec.rb' end
target()
click to toggle source
# File lib/shaf/generator/controller.rb, line 47 def target "api/controllers/#{plural_name}_controller.rb" end
template()
click to toggle source
# File lib/shaf/generator/controller.rb, line 39 def template 'api/controller.rb' end