From 87d7ed404acf7ee97f4b1c5b6178a24bb73daed3 Mon Sep 17 00:00:00 2001 From: ikeqiao Date: Tue, 29 Jul 2014 00:02:31 +0800 Subject: [PATCH] create from has_magic_columns --- .gitignore | 18 ++++++++++++++++++ .rspec | 1 + Gemfile | 8 ++++++++ LICENSE.txt | 22 ++++++++++++++++++++++ README.md | 29 +++++++++++++++++++++++++++++ Rakefile | 7 +++++++ has_magic_fields.gemspec | 25 +++++++++++++++++++++++++ lib/generators/has_magic_fields/install/USAGE | 8 ++++++++ lib/generators/has_magic_fields/install/install_generator.rb | 24 ++++++++++++++++++++++++ lib/generators/has_magic_fields/install/templates/migration.rb | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ lib/has_magic_fields.rb | 10 ++++++++++ lib/has_magic_fields/extend.rb | 173 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/has_magic_fields/models/magic_attribute.rb | 29 +++++++++++++++++++++++++++++ lib/has_magic_fields/models/magic_attribute_relationship.rb | 4 ++++ lib/has_magic_fields/models/magic_field.rb | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/has_magic_fields/models/magic_field_relationship.rb | 5 +++++ lib/has_magic_fields/models/magic_option.rb | 7 +++++++ lib/has_magic_fields/version.rb | 3 +++ rails/init.rb | 1 + spec/has_magic_fields/magic_fileds_spec.rb | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ spec/spec_helper.rb | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 21 files changed, 692 insertions(+), 0 deletions(-) create mode 100644 .gitignore create mode 100644 .rspec create mode 100644 Gemfile create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 Rakefile create mode 100644 has_magic_fields.gemspec create mode 100644 lib/generators/has_magic_fields/install/USAGE create mode 100644 lib/generators/has_magic_fields/install/install_generator.rb create mode 100644 lib/generators/has_magic_fields/install/templates/migration.rb create mode 100644 lib/has_magic_fields.rb create mode 100644 lib/has_magic_fields/extend.rb create mode 100644 lib/has_magic_fields/models/magic_attribute.rb create mode 100644 lib/has_magic_fields/models/magic_attribute_relationship.rb create mode 100644 lib/has_magic_fields/models/magic_field.rb create mode 100644 lib/has_magic_fields/models/magic_field_relationship.rb create mode 100644 lib/has_magic_fields/models/magic_option.rb create mode 100644 lib/has_magic_fields/version.rb create mode 100644 rails/init.rb create mode 100644 spec/has_magic_fields/magic_fileds_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1b16af8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,18 @@ +*.gem +*.rbc +.bundle +.config +.yardoc +Gemfile.lock +InstalledFiles +_yardoc +coverage +doc/ +lib/bundler/man +pkg +rdoc +spec/reports +test/tmp +test/version_tmp +tmp +log diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..114486d --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--color --format documentation diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..950aed5 --- /dev/null +++ b/Gemfile @@ -0,0 +1,8 @@ +source "http://ruby.taobao.org" + +gem 'rails', ' >= 4.0.0' + +group :test do + gem 'rspec-rails', ' >= 3.0.0' + gem 'sqlite3' +end diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..5ac34a9 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,22 @@ +Copyright (c) 2014 ikeqiao + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..be7780f --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +# HasMagicFields + +TODO: Write a gem description + +## Installation + +Add this line to your application's Gemfile: + + gem 'has_magic_fields' + +And then execute: + + $ bundle + +Or install it yourself as: + + $ gem install has_magic_fields + +## Usage + +TODO: Write usage instructions here + +## Contributing + +1. Fork it +2. Create your feature branch (`git checkout -b my-new-feature`) +3. Commit your changes (`git commit -am 'Add some feature'`) +4. Push to the branch (`git push origin my-new-feature`) +5. Create new Pull Request diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..643c5f8 --- /dev/null +++ b/Rakefile @@ -0,0 +1,7 @@ +require 'rake' +require "rspec" +require File.expand_path('../spec/spec_helper', __FILE__) + +task :default do + system 'bundle exec rspec spec' +end \ No newline at end of file diff --git a/has_magic_fields.gemspec b/has_magic_fields.gemspec new file mode 100644 index 0000000..124bda2 --- /dev/null +++ b/has_magic_fields.gemspec @@ -0,0 +1,25 @@ +# coding: utf-8 +lib = File.expand_path('../lib', __FILE__) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) +require 'has_magic_fields/version' + +Gem::Specification.new do |s| + s.name = "has_magic_fields" + s.version = HasMagicFields::VERSION + s.authors = ["ikeqiao"] + s.email = ["zhzsi@126.com"] + s.description = %q{TODO: Write a gem description} + s.summary = %q{TODO: Write a gem summary} + s.homepage = "" + s.license = "MIT" + + s.files = `git ls-files`.split($/) + s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) } + s.test_files = s.files.grep(%r{^(test|s|features)/}) + s.require_paths = ["lib"] + + s.add_development_dependency "bundler", "~> 1.3" + s.add_development_dependency "rake" + s.add_dependency("rails", [">= 4.0.0"]) + +end diff --git a/lib/generators/has_magic_fields/install/USAGE b/lib/generators/has_magic_fields/install/USAGE new file mode 100644 index 0000000..3e861fb --- /dev/null +++ b/lib/generators/has_magic_fields/install/USAGE @@ -0,0 +1,8 @@ +Description: + Add a migration for has_magic_fields. + +Example: + rails generate has_magic_fields:install + + This will create: + db/migrate/XXX_add_has_magic_fields_tables.rb diff --git a/lib/generators/has_magic_fields/install/install_generator.rb b/lib/generators/has_magic_fields/install/install_generator.rb new file mode 100644 index 0000000..bd26a29 --- /dev/null +++ b/lib/generators/has_magic_fields/install/install_generator.rb @@ -0,0 +1,24 @@ +require 'rails/generators/base' + +module HasMagicFields + class InstallGenerator < Rails::Generators::Base + include Rails::Generators::Migration + + source_root File.expand_path('../templates',__FILE__) + + desc "Add has_magic_fields migration." + + def self.next_migration_number(path) + unless @prev_migration_nr + @prev_migration_nr = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i + else + @prev_migration_nr += 1 + end + @prev_migration_nr.to_s + end + + def add_migration + migration_template "migration.rb", "db/migrate/add_has_magic_fields_tables.rb" + end + end +end diff --git a/lib/generators/has_magic_fields/install/templates/migration.rb b/lib/generators/has_magic_fields/install/templates/migration.rb new file mode 100644 index 0000000..df8bf52 --- /dev/null +++ b/lib/generators/has_magic_fields/install/templates/migration.rb @@ -0,0 +1,51 @@ +class AddHasMagicFieldsTables < ActiveRecord::Migration + def change + create_table :magic_fields do |t| + t.column :name, :string + t.column :pretty_name, :string + t.column :datatype, :string, :default => "string" + t.column :default, :string + t.column :is_required, :boolean, :default => false + t.column :include_blank, :boolean, :default => false + t.column :allow_other, :boolean, :default => true + t.column :created_at, :datetime + t.column :updated_at, :datetime + end + + create_table :magic_attributes do |t| + t.column :magic_field_id, :integer + t.column :magic_option_id, :integer + t.column :value, :string + t.column :created_at, :datetime + t.column :updated_at, :datetime + end + + create_table :magic_options do |t| + t.column :magic_field_id, :integer + t.column :value, :string + t.column :synonym, :string + t.column :created_at, :datetime + t.column :updated_at, :datetime + end + + create_table :magic_field_relationships do |t| + t.column :magic_field_id, :integer + t.column :owner_id, :integer + t.column :owner_type, :string + t.column :created_at, :datetime + t.column :updated_at, :datetime + end + + create_table :magic_attribute_relationships do |t| + t.column :magic_attribute_id, :integer + t.column :owner_id, :integer + t.column :owner_type, :string + end + + add_index :magic_attributes, [:magic_field_id, :magic_option_id], name:"attributes_column_option" + add_index :magic_attribute_relationships, [:magic_attribute_id, :owner_id], name:"magic_attribute_owner" + add_index :magic_field_relationships, [:magic_field_id, :owner_id], name:"magic_field_owner" + + end + +end \ No newline at end of file diff --git a/lib/has_magic_fields.rb b/lib/has_magic_fields.rb new file mode 100644 index 0000000..2113eea --- /dev/null +++ b/lib/has_magic_fields.rb @@ -0,0 +1,10 @@ +require "has_magic_fields/version" +require "has_magic_fields/models/magic_attribute" +require "has_magic_fields/models/magic_field" +require "has_magic_fields/models/magic_option" +require "has_magic_fields/models/magic_attribute_relationship" +require "has_magic_fields/models/magic_field_relationship" +require "has_magic_fields/extend" + + + diff --git a/lib/has_magic_fields/extend.rb b/lib/has_magic_fields/extend.rb new file mode 100644 index 0000000..f129664 --- /dev/null +++ b/lib/has_magic_fields/extend.rb @@ -0,0 +1,173 @@ +module HasMagicFields + module Extend extend ActiveSupport::Concern + module ClassMethods + def has_magic_fields(options = {}) + # Associations + has_many :magic_attribute_relationships, :as => :owner, :dependent => :destroy + has_many :magic_attributes, :through => :magic_attribute_relationships, :dependent => :destroy + + # Eager loading - EXPERIMENTAL! + if options[:eager] + class_eval do + def after_initialize + initialize_magic_fields + end + end + end + + # Inheritence + cattr_accessor :inherited_from + + # if options[:through] is supplied, treat as an inherited relationship + if self.inherited_from = options[:through] + class_eval do + def inherited_magic_fields + raise "Cannot inherit MagicFields from a non-existant association: #{@inherited_from}" unless self.class.method_defined?(inherited_from)# and self.send(inherited_from) + self.send(inherited_from).magic_fields + end + end + alias_method :magic_fields, :inherited_magic_fields unless method_defined? :magic_fields + + # otherwise the calling model has the relationships + else + has_many :magic_field_relationships, :as => :owner, :dependent => :destroy + has_many :magic_fields, :through => :magic_field_relationships, :dependent => :destroy + end + + # # Hook into Base + class_eval do + alias_method :reload_without_magic, :reload + alias_method :create_or_update_without_magic, :create_or_update + alias_method :read_attribute_without_magic, :read_attribute + end + + + # Add Magic to Base + alias_method :reload, :reload_with_magic + alias_method :read_attribute, :read_attribute_with_magic + alias_method :create_or_update, :create_or_update_with_magic + end + end + + included do + # Reinitialize MagicFields and MagicAttributes when Model is reloaded + def reload_with_magic + initialize_magic_fields + reload_without_magic + end + + def update_attributes(new_attributes) + attributes = new_attributes.stringify_keys + magic_attrs = magic_fields.map(&:name) + + super(attributes.select{ |k, v| !magic_attrs.include?(k) }) + attributes.select{ |k, v| magic_attrs.include?(k) }.each do |k, v| + col = find_magic_field_by_name(k) + attr = find_magic_attribute_by_column(col).first + attr.update_attributes(:value => v) + end + end + + private + + # Save MagicAttributes from @attributes + def create_or_update_with_magic + if result = create_or_update_without_magic + magic_fields.each do |column| + value = @attributes[column.name] + existing = find_magic_attribute_by_column(column) + + unless column.datatype == 'check_box_multiple' + (attr = existing.first) ? + update_magic_attribute(attr, value) : + create_magic_attribute(column, value) + else + #TODO - make this more efficient + value = [value] unless value.is_a? Array + existing.map(&:destroy) if existing + value.collect {|v| create_magic_attribute(column, v)} + end + end + end + result + end + + # Load (lazily) MagicAttributes or fall back + def method_missing(method_id, *args) + super(method_id, *args) + rescue NoMethodError + method_name = method_id.to_s + attr_names = magic_fields.map(&:name) + initialize_magic_fields and retry if attr_names.include?(method_name) or + (md = /[\?|\=]/.match(method_name) and + attr_names.include?(md.pre_match)) + super(method_id, *args) + end + + # Load the MagicAttribute(s) associated with attr_name and cast them to proper type. + def read_attribute_with_magic(attr_name) + return read_attribute_without_magic(attr_name) if column_for_attribute(attr_name) # filter for regular columns + attr_name = attr_name.to_s + + if !(value = @attributes[attr_name]).nil? + if column = find_magic_field_by_name(attr_name) + if value.is_a? Array + value.map {|v| column.type_cast(v)} + else + column.type_cast(value) + end + else + value + end + else + nil + end + end + + # Lookup all MagicAttributes and setup @attributes + def initialize_magic_fields + magic_fields.each do |column| + attribute = find_magic_attribute_by_column(column) + name = column.name + + # Validation + self.class.validates_presence_of(name) if column.is_required? + + # Write attribute + unless column.datatype == 'check_box_multiple' + (attr = attribute.first) ? + write_magic_attribute(name, attr.to_s) : + write_magic_attribute(name, column.default) + else + write_magic_attribute(name, attribute.map(&:to_s)) + end + end + end + + + def write_magic_attribute(magic_attribute, value) + @attributes[magic_attribute] = value + @attributes_cache[magic_attribute] = value + end + + def find_magic_attribute_by_column(column) + magic_attributes.to_a.find_all {|attr| attr.magic_field_id == column.id} + end + + def find_magic_field_by_name(attr_name) + magic_fields.to_a.find {|column| column.name == attr_name} + end + + def create_magic_attribute(magic_field, value) + magic_attributes << MagicAttribute.create(:magic_field => magic_field, :value => value) + end + + def update_magic_attribute(magic_attribute, value) + magic_attribute.update_attributes(:value => value) + end + end + + end +end + + diff --git a/lib/has_magic_fields/models/magic_attribute.rb b/lib/has_magic_fields/models/magic_attribute.rb new file mode 100644 index 0000000..74201f8 --- /dev/null +++ b/lib/has_magic_fields/models/magic_attribute.rb @@ -0,0 +1,29 @@ +# Always work through the interface MagicAttribute.value +class MagicAttribute < ActiveRecord::Base + belongs_to :magic_field + belongs_to :magic_option + + before_save :update_magic + + def to_s + (magic_option) ? magic_option.value : value + end + + def update_magic + if option = find_magic_option_for(value) + unless magic_option and magic_option == option + self.value = nil + self.magic_option = option + end + elsif magic_field.allow_other + self.magic_option = nil + end + end + +private + + def find_magic_option_for(value) + magic_field.magic_options.find(:first, + :conditions => ["value = ? or synonym = ?", value, value]) unless magic_field.nil? or magic_field.magic_options.blank? + end +end diff --git a/lib/has_magic_fields/models/magic_attribute_relationship.rb b/lib/has_magic_fields/models/magic_attribute_relationship.rb new file mode 100644 index 0000000..41cb69b --- /dev/null +++ b/lib/has_magic_fields/models/magic_attribute_relationship.rb @@ -0,0 +1,4 @@ +class MagicAttributeRelationship < ActiveRecord::Base + belongs_to :magic_attribute + belongs_to :owner, :polymorphic => true +end diff --git a/lib/has_magic_fields/models/magic_field.rb b/lib/has_magic_fields/models/magic_field.rb new file mode 100644 index 0000000..f017b71 --- /dev/null +++ b/lib/has_magic_fields/models/magic_field.rb @@ -0,0 +1,54 @@ +class MagicField < ActiveRecord::Base + has_many :magic_field_relationships + has_many :owners, :through => :magic_field_relationships, :as => :owner + has_many :magic_options + has_many :magic_attributes, :dependent => :destroy + + validates_presence_of :name, :datatype + validates_format_of :name, :with => /\A[a-z][a-z0-9_]+\z/ + + def type_cast(value) + begin + case datatype.to_sym + when :check_box_boolean + (value.to_int == 1) ? true : false + when :date + Date.parse(value) + when :datetime + Time.parse(value) + when :integer + value.to_int + else + value + end + rescue + value + end + end + + # Display a nicer (possibly user-defined) name for the column or use a fancified default. + def pretty_name + super || name.humanize + end + + #get or set a variable with the variable as the called method + def self.method_missing(method, *args) + debugger + + method_name = method.to_s + super(method, *args) + rescue NoMethodError + debugger + #set a value for a variable + if method_name =~ /=$/ + var_name = method_name.gsub('=', '') + value = args.first + self[var_name] = value + #retrieve a value + else + self[method_name] + end + end + + +end diff --git a/lib/has_magic_fields/models/magic_field_relationship.rb b/lib/has_magic_fields/models/magic_field_relationship.rb new file mode 100644 index 0000000..1febff2 --- /dev/null +++ b/lib/has_magic_fields/models/magic_field_relationship.rb @@ -0,0 +1,5 @@ +class MagicFieldRelationship < ActiveRecord::Base + belongs_to :magic_field + belongs_to :owner, :polymorphic => true + #belongs_to :extended_model, :polymorphic => true +end diff --git a/lib/has_magic_fields/models/magic_option.rb b/lib/has_magic_fields/models/magic_option.rb new file mode 100644 index 0000000..17e313b --- /dev/null +++ b/lib/has_magic_fields/models/magic_option.rb @@ -0,0 +1,7 @@ +class MagicOption < ActiveRecord::Base + belongs_to :magic_field + + validates_presence_of :value + validates_uniqueness_of :value, :scope => :magic_field_id + validates_uniqueness_of :synonym, :scope => :magic_field_id, :if => Proc.new{|this| !this.synonym.nil? and !this.synonym.empty?} +end \ No newline at end of file diff --git a/lib/has_magic_fields/version.rb b/lib/has_magic_fields/version.rb new file mode 100644 index 0000000..e9645b5 --- /dev/null +++ b/lib/has_magic_fields/version.rb @@ -0,0 +1,3 @@ +module HasMagicFields + VERSION = "0.0.1" +end diff --git a/rails/init.rb b/rails/init.rb new file mode 100644 index 0000000..99914fa --- /dev/null +++ b/rails/init.rb @@ -0,0 +1 @@ +require 'has_magic_fields' diff --git a/spec/has_magic_fields/magic_fileds_spec.rb b/spec/has_magic_fields/magic_fileds_spec.rb new file mode 100644 index 0000000..06525d4 --- /dev/null +++ b/spec/has_magic_fields/magic_fileds_spec.rb @@ -0,0 +1,105 @@ +require 'spec_helper' + + +describe HasMagicFields do + + context "on a single model" do + before(:each) do + @charlie = Person.create(name: "charlie") + end + + it "initializes magic fields correctly" do + expect(@charlie).not_to be(nil) + expect(@charlie.class).to be(Person) + expect(@charlie.magic_fields).not_to be(nil) + end + + it "allows adding a magic field" do + @charlie.magic_fields.create(:name => 'salary') + expect(@charlie.magic_fields.length).to be(1) + end + + it "allows setting and saving of magic attributes" do + @charlie.magic_fields.create(:name => 'salary') + @charlie.salary = 50000 + @charlie.save + @charlie = Person.find(@charlie.id) + expect(@charlie.salary).not_to be(nil) + end + + # it "forces required if is_required is true" do + # # TODO figure out why this fails + # @charlie.magic_fields.create(:name => "last_name", :is_required => true) + # expect(@charlie.save).to be(false) + # end + + it "allows datatype to be :date" do + @charlie.magic_fields.create(:name => "birthday", :datatype => :date) + @charlie.birthday = Date.today + expect(@charlie.save).to be(true) + end + + it "allows datatype to be :datetime" do + @charlie.magic_fields.create(:name => "signed_up_at", :datatype => :datetime) + @charlie.signed_up_at = DateTime.now + expect(@charlie.save).to be(true) + end + + it "allows datatype to be :integer" do + @charlie.magic_fields.create(:name => "age", :datatype => :integer) + @charlie.age = 5 + expect(@charlie.save).to be(true) + end + + it "allows datatype to be :check_box_boolean" do + @charlie.magic_fields.create(:name => "retired", :datatype => :check_box_boolean) + @charlie.retired = false + expect(@charlie.save).to be(true) + end + + it "allows default to be set" do + @charlie.magic_fields.create(:name => "bonus", :default => "40000") + expect(@charlie.bonus).to eq("40000") + end + + it "allows a pretty display name to be set" do + @charlie.magic_fields.create(:name => "zip", :pretty_name => "Zip Code") + expect(@charlie.magic_fields.last.pretty_name).to eq("Zip Code") + end + end + + context "in a parent-child relationship" do + before(:each) do + @account = Account.create(name:"important") + @alice = User.create(name:"alice", account: @account ) + end + + it "initializes magic fields correctly" do + expect(@alice).not_to be(nil) + expect(@alice.class).to be(User) + expect(@alice.magic_fields).not_to be(nil) + + expect(@account).not_to be(nil) + expect(@account.class).to be(Account) + expect(@alice.magic_fields).not_to be(nil) + end + + it "allows adding a magic field to the child" do + @alice.magic_fields.create(:name => 'salary') + expect(lambda{@alice.salary}).not_to raise_error + expect(lambda{@account.reload_with_magic.salary}).not_to raise_error + end + + it "allows adding a magic field to the parent" do + @account.magic_fields.create(:name => 'age') + expect(lambda{@alice.reload_with_magic.age}).not_to raise_error + end + + it "sets magic fields for all child models" do + @bob = User.create(name:"bob", account: @account ) + @bob.magic_fields.create(:name => 'birthday') + @alice.reload_with_magic + expect(lambda{@alice.birthday}).not_to raise_error + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..51c1a5a --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,108 @@ +require 'rubygems' +require "active_record" +require 'active_support' +require 'sqlite3' +require 'active_record/fixtures' +require 'debugger' + + +$LOAD_PATH.unshift(File.dirname(__FILE__)) +$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'rails')) +require "init" + +require "rails/railtie" + +# ActiveRecord::Base.logger = Logger.new(STDOUT) +ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:") +# ActiveRecord::Base.configurations = true + +ActiveRecord::Schema.verbose = false + +ActiveRecord::Schema.define do + + create_table "users", :force => true do |t| + t.column "name", :text + t.column "account_id", :integer + end + + create_table "accounts", :force => true do |t| + t.column "name", :text + end + + create_table "people", :force => true do |t| + t.column "name", :text + end + + + # Has Magic Columns migration generator output + + create_table :magic_fields do |t| + t.column :name, :string + t.column :pretty_name, :string + t.column :datatype, :string, :default => "string" + t.column :default, :string + t.column :is_required, :boolean, :default => false + t.column :include_blank, :boolean, :default => false + t.column :allow_other, :boolean, :default => true + t.column :created_at, :datetime + t.column :updated_at, :datetime + end + + create_table :magic_attributes do |t| + t.column :magic_field_id, :integer + t.column :magic_option_id, :integer + t.column :value, :string + t.column :created_at, :datetime + t.column :updated_at, :datetime + end + + create_table :magic_options do |t| + t.column :magic_field_id, :integer + t.column :value, :string + t.column :synonym, :string + t.column :created_at, :datetime + t.column :updated_at, :datetime + end + + create_table :magic_field_relationships do |t| + t.column :magic_field_id, :integer + t.column :owner_id, :integer + t.column :owner_type, :string + t.column :created_at, :datetime + t.column :updated_at, :datetime + end + + create_table :magic_attribute_relationships do |t| + t.column :magic_attribute_id, :integer + t.column :owner_id, :integer + t.column :owner_type, :string + end + +end + + +RSpec.configure do |config| + + config.before(:all) do + class Account < ActiveRecord::Base + include HasMagicFields::Extend + has_many :users + has_magic_fields + end + + class Person < ActiveRecord::Base + include HasMagicFields::Extend + has_magic_fields + end + + class User < ActiveRecord::Base + include HasMagicFields::Extend + belongs_to :account + has_magic_fields :through => :account + end + end + + config.after(:all) do + end +end + -- libgit2 0.21.0