Skip to content
This repository has been archived by the owner on Aug 3, 2020. It is now read-only.

New generator #19

Merged
merged 1 commit into from
Aug 22, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,22 +1,47 @@
class ActiveForce::ActiveForceModelGenerator < Rails::Generators::NamedBase
module ActiveForce
class ActiveForceModelGenerator < Rails::Generators::NamedBase

Attribute = Struct.new(:local_name, :remote_name)
source_root File.expand_path('../templates', __FILE__)

source_root File.expand_path('../templates', __FILE__)
argument :attributes, type: :array, default: [],
banner: "field[:sales_force_name] field[:sales_force_name]"
def create_model_file

def create_model_file
template "model.rb.erb", "app/models/#{file_name}.rb"
end
sf_field_names = list_field_names file_name.capitalize
@attributes = create_attributes sf_field_names

template "model.rb.erb", "app/models/#{file_name}.rb"
end

protected
protected

Attribute = Struct.new(:local_name, :remote_name)

def create_attributes sf_field_names
sf_field_names.map do |field|
Attribute.new( sf_name_to_symbol(field) ,field)
end
end

def parse_attributes! #:nodoc:
self.attributes = (attributes || []).map do |attr|
name, sales_force_name = attr.split ':', 2
remote_name = sales_force_name.presence || name
Attribute.new name, remote_name
def list_field_names table_name
Client.describe(table_name).fields.map do |field|
field.name
end
end

def sf_name_to_symbol sf_name
sf_name = sf_name.underscore
sf_name = sf_name[0..-4] if sf_name.include? "__c"
sf_name.to_sym
end

class String
def underscore
self.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr("-", "_").
downcase
end
end

end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,10 @@ require 'active_force/sobject'

class <%= class_name %> < ActiveForce::SObject

MAPPINGS = {
<% attributes.each do |attribute| -%>
<%= attribute.local_name %>: '<%= attribute.remote_name %>',
<% @attributes.each do |attribute| -%>
field :<%= attribute.local_name %>, from: '<%= attribute.remote_name %>',
<% end -%>
id: 'Id'
}
FIELDS = MAPPINGS.values
RELATED_LIST_FIELDS = FIELDS

self.mappings = MAPPINGS
self.fields = FIELDS
self.table_name = '<%= class_name %>'

<% attributes.each do |attribute| -%>
attribute :<%= attribute.local_name %>
<% end -%>

end