Skip to content

Commit

Permalink
Merge branch 'custom_fields' of github.com:fatfreecrm/fat_free_crm in…
Browse files Browse the repository at this point in the history
…to custom_fields
  • Loading branch information
ndbroadbent committed Nov 17, 2011
2 parents 82cfb5a + aeb41ec commit 5ee12a1
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/fields/field.rb
Expand Up @@ -16,7 +16,7 @@ class Field < ActiveRecord::Base
'select' => :string,
'radio' => :string,
'check_boxes' => :text,
'checkbox' => :boolean,
'boolean' => :boolean,
'date' => :date,
'datetime' => :timestamp,
'decimal' => [:decimal, {:precision => 15, :scale => 2}],
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20111116091952_add_field_groups_tag_id.rb
@@ -0,0 +1,9 @@
class AddFieldGroupsTagId < ActiveRecord::Migration
def self.up
add_column :field_groups, :tag_id, :integer
end

def self.down
remove_column :field_groups, :tag_id
end
end
9 changes: 9 additions & 0 deletions db/migrate/20111117041311_change_fields_collection_to_text.rb
@@ -0,0 +1,9 @@
class ChangeFieldsCollectionToText < ActiveRecord::Migration
def self.up
change_column :fields, :collection, :text
end

def self.down
change_column :fields, :collection, :string
end
end
108 changes: 108 additions & 0 deletions lib/tasks/super_tags.rake
@@ -0,0 +1,108 @@
namespace :super_tags do

desc "Migrate super_tags plugin to core custom_fields"
task :migrate => :environment do
def dryrun?
#~ true
ENV['DRYRUN'] == 'true'
end

columns = %w(tag_id field_name field_type field_label table_name select_options max_size required disabled form_field_type field_info)
map_as = {
'short_answer' => 'string',
'number' => 'decimal',
'long_answer' => 'text',
'select_list' => 'select',
'multi_select' => 'check_boxes',
'checkbox' => 'boolean',
'date' => 'date',
'datetime' => 'datetime'
}
group_ids = {}
updates = []

connection = ActiveRecord::Base.connection

field_data = connection.select_all "SELECT #{columns.join(', ')} FROM customfields"

tag_ids = field_data.map {|row| row['tag_id']}.uniq
tag_ids.each do |tag_id|
tag = ActsAsTaggableOn::Tag.find(tag_id)

if (data = connection.select_all "SELECT * FROM tag#{tag_id}s").present?
keys = data.first.keys.reject {|k| %w(id customizable_id customizable_type).include?(k)}

# FieldGroup
unless field_group = FieldGroup.find_by_tag_id(tag.id)
group_params = {:tag_id => tag.id, :name => tag.name + ' Details'}
if dryrun?
puts group_params
else
field_group = FieldGroup.create! group_params
end
end

klass_names = data.map {|row| row['customizable_type']}.uniq
klass_names.each do |klass_name|
klass = klass_name.constantize

# CustomField
field_data.each do |row|
next unless row['tag_id'] == tag_id

unless field = CustomField.find_by_klass_name_and_name(klass_name, row['field_name'])

collection = row['select_options'].split('|').map(&:strip) if row['select_options']

field_params = {
:klass_name => klass_name,
:name => row['field_name'],
:label => row['field_label'],
:position => row['position'],
:collection => collection,
:as => map_as[row['form_field_type']],
:hint => row['field_info'],
:required => row['required'],
:disabled => row['disabled'],
:maxlength => row['max_size'],
:field_group_id => field_group.try(:id)
}
if dryrun?
puts field_params
else
field = CustomField.create! field_params
end
end
end

# Data
data.each do |row|
values = []
keys.each do |key|
next unless klass.column_names.include?(key)

value = if row[key] =~ /^\d+$/
row[key]
elsif row[key].present?
connection.quote(row[key])
end
values << "#{key} = #{value}" if value.present?
end

updates << "UPDATE #{klass.table_name} SET #{values.join(', ')} WHERE #{klass.primary_key} = #{row['customizable_id']}" if values.present?
end
end
end
end

if dryrun?
File.open('update.sql', 'w') do |file|
file << updates.join(";\n")
end
else
updates.each do |update|
connection.execute update
end
end
end
end

0 comments on commit 5ee12a1

Please sign in to comment.