module PowerApi::GeneratorHelper::ActiveRecordResource

rubocop:disable Metrics/ModuleLength

Public Instance Methods

attributes_names() click to toggle source
# File lib/power_api/generator_helper/active_record_resource.rb, line 74
def attributes_names
  extract_attrs_names(resource_attributes)
end
attributes_symbols_text_list() click to toggle source
# File lib/power_api/generator_helper/active_record_resource.rb, line 100
def attributes_symbols_text_list
  attrs_to_symobls_text_list(attributes_names)
end
camel() click to toggle source
# File lib/power_api/generator_helper/active_record_resource.rb, line 42
def camel
  resource_name.camelize
end
camel_plural() click to toggle source
# File lib/power_api/generator_helper/active_record_resource.rb, line 46
def camel_plural
  camel.pluralize
end
class_definition_line() click to toggle source
# File lib/power_api/generator_helper/active_record_resource.rb, line 70
def class_definition_line
  "class #{camel} < ApplicationRecord\n"
end
id() click to toggle source
# File lib/power_api/generator_helper/active_record_resource.rb, line 30
def id
  "#{snake_case}_id"
end
optional_resource_attributes() click to toggle source
# File lib/power_api/generator_helper/active_record_resource.rb, line 96
def optional_resource_attributes
  permitted_attributes.reject { |attr| attr[:required] }
end
path() click to toggle source
# File lib/power_api/generator_helper/active_record_resource.rb, line 66
def path
  "app/models/#{snake_case}.rb"
end
permitted_attributes() click to toggle source
# File lib/power_api/generator_helper/active_record_resource.rb, line 86
def permitted_attributes
  resource_attributes.reject do |attr|
    [:created_at, :updated_at].include?(attr[:name])
  end
end
permitted_attributes_names() click to toggle source
# File lib/power_api/generator_helper/active_record_resource.rb, line 82
def permitted_attributes_names
  extract_attrs_names(permitted_attributes)
end
permitted_attributes_symbols_text_list() click to toggle source
# File lib/power_api/generator_helper/active_record_resource.rb, line 104
def permitted_attributes_symbols_text_list
  attrs_to_symobls_text_list(permitted_attributes_names)
end
plural() click to toggle source
# File lib/power_api/generator_helper/active_record_resource.rb, line 50
def plural
  snake_case.pluralize
end
plural_titleized() click to toggle source
# File lib/power_api/generator_helper/active_record_resource.rb, line 62
def plural_titleized
  plural.titleize
end
required_attributes_names() click to toggle source
# File lib/power_api/generator_helper/active_record_resource.rb, line 78
def required_attributes_names
  extract_attrs_names(required_resource_attributes)
end
required_resource_attributes() click to toggle source
# File lib/power_api/generator_helper/active_record_resource.rb, line 92
def required_resource_attributes
  permitted_attributes.select { |attr| attr[:required] }
end
resource_attributes=(collection) click to toggle source
# File lib/power_api/generator_helper/active_record_resource.rb, line 23
def resource_attributes=(collection)
  attributes = format_attributes(collection)
  raise PowerApi::GeneratorError.new("at least one attribute must be added") if attributes.none?

  @resource_attributes = attributes
end
resource_name=(value) click to toggle source
# File lib/power_api/generator_helper/active_record_resource.rb, line 9
def resource_name=(value)
  @resource_name = value

  if !resource_class
    raise PowerApi::GeneratorError.new(
      "Invalid resource name. Must be the snake_case representation of a ruby class"
    )
  end

  if !resource_is_active_record_model?
    raise PowerApi::GeneratorError.new("resource is not an active record model")
  end
end
snake_case() click to toggle source
# File lib/power_api/generator_helper/active_record_resource.rb, line 54
def snake_case
  resource_name.underscore
end
titleized() click to toggle source
# File lib/power_api/generator_helper/active_record_resource.rb, line 58
def titleized
  resource_name.titleize
end
upcase() click to toggle source
# File lib/power_api/generator_helper/active_record_resource.rb, line 34
def upcase
  snake_case.upcase
end
upcase_plural() click to toggle source
# File lib/power_api/generator_helper/active_record_resource.rb, line 38
def upcase_plural
  plural.upcase
end

Private Instance Methods

attrs_to_symobls_text_list(attrs) click to toggle source
# File lib/power_api/generator_helper/active_record_resource.rb, line 118
def attrs_to_symobls_text_list(attrs)
  attrs.map { |a| ":#{a}" }.join(",\n") + "\n"
end
extract_attrs_names(attrs) click to toggle source
# File lib/power_api/generator_helper/active_record_resource.rb, line 114
def extract_attrs_names(attrs)
  attrs.map { |attr| attr[:name] }
end
format_attributes(attrs) click to toggle source
# File lib/power_api/generator_helper/active_record_resource.rb, line 122
def format_attributes(attrs)
  columns = resource_class.columns.inject([]) do |memo, col|
    col_name = col.name.to_sym
    next memo if col_name == :id

    memo << {
      name: col_name,
      type: col.type,
      swagger_type: get_swagger_type(col.type),
      required: required_attribute?(col_name),
      example: get_attribute_example(col.type, col_name)
    }

    memo
  end

  return columns if attrs.blank?

  attrs = attrs.map(&:to_sym)
  columns.select { |col| attrs.include?(col[:name]) }
end
get_attribute_example(data_type, col_name) click to toggle source
# File lib/power_api/generator_helper/active_record_resource.rb, line 157
def get_attribute_example(data_type, col_name)
  case data_type
  when :date
    "'1984-06-04'"
  when :datetime
    "'1984-06-04 09:00'"
  when :integer
    666
  when :float, :decimal
    6.66
  when :boolean
    true
  else
    "'Some #{col_name}'"
  end
end
get_swagger_type(data_type) click to toggle source
# File lib/power_api/generator_helper/active_record_resource.rb, line 144
def get_swagger_type(data_type)
  case data_type
  when :integer
    :integer
  when :float, :decimal
    :float
  when :boolean
    :boolean
  else
    :string
  end
end
required_attribute?(col_name) click to toggle source
# File lib/power_api/generator_helper/active_record_resource.rb, line 174
def required_attribute?(col_name)
  validator_names = resource_class.validators_on(col_name).map do |validator|
    validator.class.name
  end

  validator_names.include?("ActiveRecord::Validations::PresenceValidator")
end
resource_class() click to toggle source
# File lib/power_api/generator_helper/active_record_resource.rb, line 182
def resource_class
  resource_name.classify.constantize
rescue NameError
  false
end
resource_is_active_record_model?() click to toggle source
# File lib/power_api/generator_helper/active_record_resource.rb, line 188
def resource_is_active_record_model?
  !!ActiveRecord::Base.descendants.find { |model_class| model_class == resource_class }
end
resource_name() click to toggle source
# File lib/power_api/generator_helper/active_record_resource.rb, line 110
def resource_name
  @resource_name
end