class ActiveForce::ModelGenerator

Constants

Attribute
SALESFORCE_TO_ACTIVEMODEL_TYPE_MAP

Public Instance Methods

create_model_file() click to toggle source
# File lib/generators/active_force/model/model_generator.rb, line 18
def create_model_file
  @table_name = name
  @class_name = prepare_namespace + @table_name.gsub('__c', '')
  template "model.rb.erb", "app/models/#{@class_name.underscore}.rb" if table_exists?
end

Protected Instance Methods

add_type(type) click to toggle source
# File lib/generators/active_force/model/model_generator.rb, line 63
def add_type(type)
  # String is the default so no need to add it
  return '' if type == :string
  ", as: :#{ type }"
end
attribute_line(attribute) click to toggle source
# File lib/generators/active_force/model/model_generator.rb, line 53
def attribute_line attribute
  "field :#{ attribute.field },#{ space_justify attribute.field }  from: '#{ attribute.column }'#{ add_type(attribute.type) }"
end
attributes() click to toggle source
# File lib/generators/active_force/model/model_generator.rb, line 32
def attributes
  @attributes ||= sfdc_columns.sort_by { |col| col[:name].downcase }.map do |column|
    Attribute.new column_to_field(column.name), column.name, saleforce_to_active_model_type(column.type)
  end
  @attributes - [:id]
end
column_to_field(column) click to toggle source
# File lib/generators/active_force/model/model_generator.rb, line 49
def column_to_field column
  column.underscore.gsub("__c", "").to_sym
end
prepare_namespace() click to toggle source
# File lib/generators/active_force/model/model_generator.rb, line 26
def prepare_namespace
  @namespace.present? ? @namespace + '::' : @namespace
end
saleforce_to_active_model_type(type) click to toggle source
# File lib/generators/active_force/model/model_generator.rb, line 69
def saleforce_to_active_model_type type
  SALESFORCE_TO_ACTIVEMODEL_TYPE_MAP.fetch(type, :string)
end
sfdc_columns() click to toggle source
# File lib/generators/active_force/model/model_generator.rb, line 39
def sfdc_columns
  @columns ||= ActiveForce::SObject.sfdc_client.describe(@table_name).fields
end
space_justify(field_name) click to toggle source
# File lib/generators/active_force/model/model_generator.rb, line 57
def space_justify field_name
  longest_field = attributes.map { |attr| attr.field.length } .max
  justify_count = longest_field - field_name.length
  " " * justify_count
end
table_exists?() click to toggle source
# File lib/generators/active_force/model/model_generator.rb, line 43
def table_exists?
  !! sfdc_columns
  rescue Faraday::ResourceNotFound
    puts "The specified table name is not found. Be sure to append __c if it's custom"
end