class Graphiti::ResourceGenerator
Public Instance Methods
generate_all()
click to toggle source
# File lib/generators/graphiti/resource_generator.rb, line 36 def generate_all generate_model generate_controller generate_application_resource unless application_resource_defined? generate_route generate_resource generate_resource_specs generate_api_specs end
Private Instance Methods
application_resource_defined?()
click to toggle source
# File lib/generators/graphiti/resource_generator.rb, line 136 def application_resource_defined? "ApplicationResource".safe_constantize.present? end
attributes_class()
click to toggle source
# File lib/generators/graphiti/resource_generator.rb, line 73 def attributes_class return @attributes_class if @attributes_class case @options["attributes-from"] # thor will set the value to the key if no value is specified when "attributes-from" klass = class_name when :kind_of?, String klass = @options["attributes-from"].classify else # return nil if attributes-from isn't set or has an invalid value return end begin @attributes_class = klass.safe_constantize rescue NameError raise NameError, "attributes-from #{klass.inspect} does not exist." end end
create?()
click to toggle source
# File lib/generators/graphiti/resource_generator.rb, line 169 def create? behavior == :invoke end
default_attributes()
click to toggle source
Generates a list of OpenStruct(:name, :type) objects that map to the attributes_class
columns.
# File lib/generators/graphiti/resource_generator.rb, line 97 def default_attributes unless attributes_class.is_a?(Class) && attributes_class <= ApplicationRecord raise "Unable to set #{self} default_attributes from #{attributes_class}. #{attributes_class} must be a kind of ApplicationRecord" end if attributes_class.table_exists? return attributes_class.columns.map do |c| OpenStruct.new({name: c.name.to_sym, type: c.type}) end else raise "#{attributes_class} table must exist. Please run migrations." end end
generate_api_specs()
click to toggle source
# File lib/generators/graphiti/resource_generator.rb, line 156 def generate_api_specs opts = {} opts[:actions] = @options[:actions] if @options[:actions] opts[:rawid] = @options[:rawid] if @options[:rawid] invoke "graphiti:api_test", [resource_klass], opts end
generate_application_resource()
click to toggle source
# File lib/generators/graphiti/resource_generator.rb, line 130 def generate_application_resource to = File.join("app/resources", class_path, "application_resource.rb") template("application_resource.rb.erb", to) require "#{::Rails.root}/#{to}" end
generate_controller()
click to toggle source
# File lib/generators/graphiti/resource_generator.rb, line 125 def generate_controller to = File.join("app/controllers", class_path, "#{file_name.pluralize}_controller.rb") template("controller.rb.erb", to) end
generate_model()
click to toggle source
# File lib/generators/graphiti/resource_generator.rb, line 65 def generate_model action(ModelAction.new(class_name)) end
generate_resource()
click to toggle source
# File lib/generators/graphiti/resource_generator.rb, line 163 def generate_resource to = File.join("app/resources", class_path, "#{file_name}_resource.rb") template("resource.rb.erb", to) require "#{::Rails.root}/#{to}" if create? end
generate_resource_specs()
click to toggle source
# File lib/generators/graphiti/resource_generator.rb, line 149 def generate_resource_specs opts = {} opts[:actions] = @options[:actions] if @options[:actions] opts[:rawid] = @options[:rawid] if @options[:rawid] invoke "graphiti:resource_test", [resource_klass], opts end
generate_route()
click to toggle source
# File lib/generators/graphiti/resource_generator.rb, line 140 def generate_route code = "resources :#{file_name.pluralize}" code << %(, only: [#{actions.map { |a| ":#{a}" }.join(", ")}]) if actions.length < 5 code << "\n" inject_into_file "config/routes.rb", after: /ApplicationResource.*$\n/ do indent(code, 4) end end
model_klass()
click to toggle source
# File lib/generators/graphiti/resource_generator.rb, line 173 def model_klass class_name.safe_constantize end
omit_comments?()
click to toggle source
# File lib/generators/graphiti/resource_generator.rb, line 69 def omit_comments? @options["omit-comments"] end
resource_attributes()
click to toggle source
# File lib/generators/graphiti/resource_generator.rb, line 110 def resource_attributes # set a temporary variable because overriding attributes causes # weird behavior when the generator is run. It will override # everytime regardless of the conditional. if !attributes_class.nil? default_attributes else attributes end end
resource_klass()
click to toggle source
# File lib/generators/graphiti/resource_generator.rb, line 177 def resource_klass "#{model_klass}Resource" end
responders?()
click to toggle source
# File lib/generators/graphiti/resource_generator.rb, line 121 def responders? defined?(::Responders) end
type()
click to toggle source
# File lib/generators/graphiti/resource_generator.rb, line 181 def type model_klass.name.underscore.pluralize end