Permalink
Browse files

Majors changes, changed the "core" work. WARNING: not work for now

  • Loading branch information...
1 parent c5bc683 commit 1b76cf2c5dbb278e4f9a3f854e59a68214d84df0 @anga committed Jan 6, 2012
@@ -0,0 +1,60 @@
+module ExtendModelAt
+ class ModelManager
+ def initialize(column_name,model, config)
+ @column_name, @model, @config = column_name, model, config
+ end
+
+ def assign(column,value)
+ raise "#{value} is not valid" if value.class != @config[column.to_sym][:type]
+
+ last_model = get_column_model column
+ type_class = get_type_class @config[column.to_sym][:type]
+
+ if @extend_at.nil?
+ @extend_at = ExtendAt.new :model => @model
+ @extend_at.save
+ end
+
+ if last_model.nil?
+ eval "@extend_at.columns << #{type_class}.new :column => column, :value => value"
+ else
+ eval "last_model.value = value
+ last_model.save"
+ end
+ end
+
+ def get_value(column)
+ model = get_column_model column, @config[column.to_sym][:type]
+ model.value
+ end
+
+ protected
+ def get_column_model(column)
+ type_class = get_type_class @config[column.to_sym][:type]
+ eval "#{type_class}.where(
+ #{type_class}.arel_table[:column].eq(column).and(
+ #{type_class}.arel_table[:extend_at_column_id].in(
+ Column.arel_table.project(:id).where(
+ Column.arel_table[:extend_at_id].eq(@extend_at.id)
+ )
+ )
+ )
+ ).try(:first)"
+ end
+
+ def get_type_class(type)
+ type = type
+ if type == Fixnum
+ return "IntegerValue"
+ elsif type == Float
+ return "FloatValue"
+ elsif type == String
+ return "StringValue"
+ elsif type == :text
+ return "TextValue"
+ else
+ return "AnyValue"
+ end
+ end
+ end
+end
@@ -0,0 +1,5 @@
+class AnyValue < ActiveRecord::Base
+ set_table_name "extend_at_anies"
+ belongs_to :extend_at, :class_name => 'ExtendAt'
+end
+
@@ -0,0 +1,7 @@
+class Column < ActiveRecord::Base
+ set_table_name "extend_at_columns"
+ belongs_to :extend_at
+ belongs_to :column, :polymorphic => true
+ belongs_to :extend_at, :class_name => 'ExtendAt'
+end
+
@@ -0,0 +1,6 @@
+class ExtendAt < ActiveRecord::Base
+ set_table_name "extend_ats"
+ belons_to :model, :polymorphic => true
+ has_many :extend_at_columns
+ has_many :columns, :through => :extend_at_columns
+end
@@ -0,0 +1,4 @@
+class FloatValue < ActiveRecord::Base
+ set_table_name "extend_at_floats"
+ belongs_to :extend_at, :class_name => 'ExtendAt'
+end
@@ -0,0 +1,4 @@
+class IntegerValue < ActiveRecord::Base
+ set_table_name "extend_at_integers"
+ belongs_to :extend_at, :class_name => 'ExtendAt'
+end
@@ -0,0 +1,5 @@
+class StringValue < ActiveRecord::Base
+ set_table_name "extend_at_strings"
+ belongs_to :extend_at, :class_name => 'ExtendAt'
+end
+
@@ -0,0 +1,5 @@
+class TextValue < ActiveRecord::Base
+ set_table_name "extend_at_texts"
+ belongs_to :extend_at, :class_name => 'ExtendAt'
+end
+
@@ -0,0 +1,8 @@
+Description:
+ Install extend_at gem
+
+Example:
+ rails generate extend_at Thing
+
+ This will create:
+ db/migrate/extend_at_tables.rb
@@ -0,0 +1,22 @@
+module ExtendAt
+ module Generators
+ class InstallGenerator < Rails::Generators::NamedBase
+ include Rails::Generators::Migration
+ source_root File.expand_path('./templates', __FILE__)
+ desc "Generate all necesaries models and migrations for extend_at gem"
+
+ def self.next_migration_number(path)
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
+ end
+
+ def create_model_file
+ template "integer_value.rb", "app/models/integer_value.rb"
+ template "float_value.rb", "app/models/float_value.rb"
+ template "string_value.rb", "app/models/string_value.rb"
+ template "text_value.rb", "app/models/any_value.rb"
+ template "any_value.rb", "app/models/any_value.rb"
+ migration_template "create_extend_at_tables.rb", "db/migrate/create_extend_at_tables.rb"
+ end
+ end
+ end
+end
@@ -0,0 +1,51 @@
+class ExtendAtTables < ActiveRecord::Migration
+ def up
+ create_table :extend_ats do |t|
+ t.references :model, :polymorphic => true
+ end
+
+ create_table :extend_at_integers do |t|
+ t.belongs_to :extend_at_column
+ t.string :column
+ t.integer :value
+ end
+
+ create_table :extend_at_floats do |t|
+ t.belongs_to :extend_at_column
+ t.string :column
+ t.text :value
+ end
+
+ create_table :extend_at_strings do |t|
+ t.belongs_to :extend_at_column
+ t.string :column
+ t.string :value
+ end
+
+ create_table :extend_at_texts do |t|
+ t.belongs_to :extend_at_column
+ t.string :column
+ t.string :value
+ end
+
+ create_table :extend_at_anies do |t|
+ t.belongs_to :extend_at_column
+ t.string :column
+ t.text :value
+ end
+
+ create_table :extend_at_columns do |t|
+ t.belongs_to :extend_at
+ t.belongs_to :column, :polymorphic => true
+ end
+ end
+
+ def down
+ drop_table :extend_at
+ drop_table :extend_at_integers
+ drop_table :extend_at_floats
+ drop_table :extend_at_strings
+ drop_table :extend_at_texts
+ drop_table :extend_at_anies
+ end
+end

0 comments on commit 1b76cf2

Please sign in to comment.