From 5b7837db063f00d05c4b2d75cc669a6e509cee73 Mon Sep 17 00:00:00 2001 From: ikeqiao Date: Wed, 30 Jul 2014 16:10:52 +0800 Subject: [PATCH] Move lib/has_magic_fields/models to lib/app/models per rails convention --- lib/app/models/magic_attribute.rb | 29 +++++++++++++++++++++++++++++ lib/app/models/magic_attribute_relationship.rb | 4 ++++ lib/app/models/magic_field.rb | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/app/models/magic_field_relationship.rb | 5 +++++ lib/app/models/magic_option.rb | 7 +++++++ lib/has_magic_fields.rb | 5 ----- lib/has_magic_fields/extend.rb | 11 +++++++++++ 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 ------- spec/has_magic_fields/magic_fileds_spec.rb | 8 ++++++++ spec/spec_helper.rb | 15 +++++++++++---- 14 files changed, 130 insertions(+), 108 deletions(-) create mode 100644 lib/app/models/magic_attribute.rb create mode 100644 lib/app/models/magic_attribute_relationship.rb create mode 100644 lib/app/models/magic_field.rb create mode 100644 lib/app/models/magic_field_relationship.rb create mode 100644 lib/app/models/magic_option.rb delete mode 100644 lib/has_magic_fields/models/magic_attribute.rb delete mode 100644 lib/has_magic_fields/models/magic_attribute_relationship.rb delete mode 100644 lib/has_magic_fields/models/magic_field.rb delete mode 100644 lib/has_magic_fields/models/magic_field_relationship.rb delete mode 100644 lib/has_magic_fields/models/magic_option.rb diff --git a/lib/app/models/magic_attribute.rb b/lib/app/models/magic_attribute.rb new file mode 100644 index 0000000..74201f8 --- /dev/null +++ b/lib/app/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/app/models/magic_attribute_relationship.rb b/lib/app/models/magic_attribute_relationship.rb new file mode 100644 index 0000000..41cb69b --- /dev/null +++ b/lib/app/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/app/models/magic_field.rb b/lib/app/models/magic_field.rb new file mode 100644 index 0000000..c142ed9 --- /dev/null +++ b/lib/app/models/magic_field.rb @@ -0,0 +1,55 @@ +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 + 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/app/models/magic_field_relationship.rb b/lib/app/models/magic_field_relationship.rb new file mode 100644 index 0000000..1febff2 --- /dev/null +++ b/lib/app/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/app/models/magic_option.rb b/lib/app/models/magic_option.rb new file mode 100644 index 0000000..17e313b --- /dev/null +++ b/lib/app/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.rb b/lib/has_magic_fields.rb index 2113eea..b07f3ea 100644 --- a/lib/has_magic_fields.rb +++ b/lib/has_magic_fields.rb @@ -1,9 +1,4 @@ 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 index a61e45d..381b02e 100644 --- a/lib/has_magic_fields/extend.rb +++ b/lib/has_magic_fields/extend.rb @@ -1,3 +1,6 @@ +require 'active_support' +require 'active_record' + module HasMagicFields module Extend extend ActiveSupport::Concern include ActiveModel::Validations @@ -91,6 +94,14 @@ module HasMagicFields end end + + %w{ models }.each do |dir| + path = File.join(File.dirname(__FILE__), '../app', dir) + $LOAD_PATH << path + ActiveSupport::Dependencies.autoload_paths << path + ActiveSupport::Dependencies.autoload_once_paths.delete(path) + end + end end diff --git a/lib/has_magic_fields/models/magic_attribute.rb b/lib/has_magic_fields/models/magic_attribute.rb deleted file mode 100644 index 74201f8..0000000 --- a/lib/has_magic_fields/models/magic_attribute.rb +++ /dev/null @@ -1,29 +0,0 @@ -# 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 deleted file mode 100644 index 41cb69b..0000000 --- a/lib/has_magic_fields/models/magic_attribute_relationship.rb +++ /dev/null @@ -1,4 +0,0 @@ -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 deleted file mode 100644 index f017b71..0000000 --- a/lib/has_magic_fields/models/magic_field.rb +++ /dev/null @@ -1,54 +0,0 @@ -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 deleted file mode 100644 index 1febff2..0000000 --- a/lib/has_magic_fields/models/magic_field_relationship.rb +++ /dev/null @@ -1,5 +0,0 @@ -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 deleted file mode 100644 index 17e313b..0000000 --- a/lib/has_magic_fields/models/magic_option.rb +++ /dev/null @@ -1,7 +0,0 @@ -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/spec/has_magic_fields/magic_fileds_spec.rb b/spec/has_magic_fields/magic_fileds_spec.rb index 1e88987..b0d0f44 100644 --- a/spec/has_magic_fields/magic_fileds_spec.rb +++ b/spec/has_magic_fields/magic_fileds_spec.rb @@ -100,6 +100,14 @@ describe HasMagicFields do @bob = User.create(name:"bob", account: @account ) @bob.magic_fields.create(:name => 'birthday') expect(lambda{@alice.birthday}).not_to raise_error + @bob.birthday = "2014-07-29" + expect(@bob.save).to be(true) + expect(@account.birthday).to be(nil) + expect(@alice.birthday).to be(nil) + @alice.birthday = "2013-07-29" + expect(@alice.save).to be(true) + expect(@alice.birthday).not_to eq(@bob.birthday) end + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 51c1a5a..0479da0 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,10 +1,11 @@ + +require 'debugger' + require 'rubygems' +require 'rspec/autorun' 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')) @@ -12,7 +13,13 @@ require "init" require "rails/railtie" -# ActiveRecord::Base.logger = Logger.new(STDOUT) +module Rails + def self.env + @_env ||= ActiveSupport::StringInquirer.new(ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development") + end +end + + ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:") # ActiveRecord::Base.configurations = true -- libgit2 0.21.0