Commit 5b7837db063f00d05c4b2d75cc669a6e509cee73

Authored by ikeqiao
1 parent c595c46c
Exists in master

Move lib/has_magic_fields/models to lib/app/models per rails convention

lib/app/models/magic_attribute.rb 0 โ†’ 100644
... ... @@ -0,0 +1,29 @@
  1 +# Always work through the interface MagicAttribute.value
  2 +class MagicAttribute < ActiveRecord::Base
  3 + belongs_to :magic_field
  4 + belongs_to :magic_option
  5 +
  6 + before_save :update_magic
  7 +
  8 + def to_s
  9 + (magic_option) ? magic_option.value : value
  10 + end
  11 +
  12 + def update_magic
  13 + if option = find_magic_option_for(value)
  14 + unless magic_option and magic_option == option
  15 + self.value = nil
  16 + self.magic_option = option
  17 + end
  18 + elsif magic_field.allow_other
  19 + self.magic_option = nil
  20 + end
  21 + end
  22 +
  23 +private
  24 +
  25 + def find_magic_option_for(value)
  26 + magic_field.magic_options.find(:first,
  27 + :conditions => ["value = ? or synonym = ?", value, value]) unless magic_field.nil? or magic_field.magic_options.blank?
  28 + end
  29 +end
... ...
lib/app/models/magic_attribute_relationship.rb 0 โ†’ 100644
... ... @@ -0,0 +1,4 @@
  1 +class MagicAttributeRelationship < ActiveRecord::Base
  2 + belongs_to :magic_attribute
  3 + belongs_to :owner, :polymorphic => true
  4 +end
... ...
lib/app/models/magic_field.rb 0 โ†’ 100644
... ... @@ -0,0 +1,55 @@
  1 +class MagicField < ActiveRecord::Base
  2 + has_many :magic_field_relationships
  3 + has_many :owners, :through => :magic_field_relationships, :as => :owner
  4 + has_many :magic_options
  5 + has_many :magic_attributes, :dependent => :destroy
  6 +
  7 + validates_presence_of :name, :datatype
  8 + validates_format_of :name, :with => /\A[a-z][a-z0-9_]+\z/
  9 +
  10 + def type_cast(value)
  11 + begin
  12 + case datatype.to_sym
  13 + when :check_box_boolean
  14 + when :check_box_boolean
  15 + (value.to_int == 1) ? true : false
  16 + when :date
  17 + Date.parse(value)
  18 + when :datetime
  19 + Time.parse(value)
  20 + when :integer
  21 + value.to_int
  22 + else
  23 + value
  24 + end
  25 + rescue
  26 + value
  27 + end
  28 + end
  29 +
  30 + # Display a nicer (possibly user-defined) name for the column or use a fancified default.
  31 + def pretty_name
  32 + super || name.humanize
  33 + end
  34 +
  35 + #get or set a variable with the variable as the called method
  36 + def self.method_missing(method, *args)
  37 + debugger
  38 +
  39 + method_name = method.to_s
  40 + super(method, *args)
  41 + rescue NoMethodError
  42 + debugger
  43 + #set a value for a variable
  44 + if method_name =~ /=$/
  45 + var_name = method_name.gsub('=', '')
  46 + value = args.first
  47 + self[var_name] = value
  48 + #retrieve a value
  49 + else
  50 + self[method_name]
  51 + end
  52 + end
  53 +
  54 +
  55 +end
... ...
lib/app/models/magic_field_relationship.rb 0 โ†’ 100644
... ... @@ -0,0 +1,5 @@
  1 +class MagicFieldRelationship < ActiveRecord::Base
  2 + belongs_to :magic_field
  3 + belongs_to :owner, :polymorphic => true
  4 + #belongs_to :extended_model, :polymorphic => true
  5 +end
... ...
lib/app/models/magic_option.rb 0 โ†’ 100644
... ... @@ -0,0 +1,7 @@
  1 +class MagicOption < ActiveRecord::Base
  2 + belongs_to :magic_field
  3 +
  4 + validates_presence_of :value
  5 + validates_uniqueness_of :value, :scope => :magic_field_id
  6 + validates_uniqueness_of :synonym, :scope => :magic_field_id, :if => Proc.new{|this| !this.synonym.nil? and !this.synonym.empty?}
  7 +end
0 8 \ No newline at end of file
... ...
lib/has_magic_fields.rb
1 1 require "has_magic_fields/version"
2   -require "has_magic_fields/models/magic_attribute"
3   -require "has_magic_fields/models/magic_field"
4   -require "has_magic_fields/models/magic_option"
5   -require "has_magic_fields/models/magic_attribute_relationship"
6   -require "has_magic_fields/models/magic_field_relationship"
7 2 require "has_magic_fields/extend"
8 3  
9 4  
... ...
lib/has_magic_fields/extend.rb
  1 +require 'active_support'
  2 +require 'active_record'
  3 +
1 4 module HasMagicFields
2 5 module Extend extend ActiveSupport::Concern
3 6 include ActiveModel::Validations
... ... @@ -91,6 +94,14 @@ module HasMagicFields
91 94 end
92 95 end
93 96  
  97 +
  98 + %w{ models }.each do |dir|
  99 + path = File.join(File.dirname(__FILE__), '../app', dir)
  100 + $LOAD_PATH << path
  101 + ActiveSupport::Dependencies.autoload_paths << path
  102 + ActiveSupport::Dependencies.autoload_once_paths.delete(path)
  103 + end
  104 +
94 105 end
95 106 end
96 107  
... ...
lib/has_magic_fields/models/magic_attribute.rb
... ... @@ -1,29 +0,0 @@
1   -# Always work through the interface MagicAttribute.value
2   -class MagicAttribute < ActiveRecord::Base
3   - belongs_to :magic_field
4   - belongs_to :magic_option
5   -
6   - before_save :update_magic
7   -
8   - def to_s
9   - (magic_option) ? magic_option.value : value
10   - end
11   -
12   - def update_magic
13   - if option = find_magic_option_for(value)
14   - unless magic_option and magic_option == option
15   - self.value = nil
16   - self.magic_option = option
17   - end
18   - elsif magic_field.allow_other
19   - self.magic_option = nil
20   - end
21   - end
22   -
23   -private
24   -
25   - def find_magic_option_for(value)
26   - magic_field.magic_options.find(:first,
27   - :conditions => ["value = ? or synonym = ?", value, value]) unless magic_field.nil? or magic_field.magic_options.blank?
28   - end
29   -end
lib/has_magic_fields/models/magic_attribute_relationship.rb
... ... @@ -1,4 +0,0 @@
1   -class MagicAttributeRelationship < ActiveRecord::Base
2   - belongs_to :magic_attribute
3   - belongs_to :owner, :polymorphic => true
4   -end
lib/has_magic_fields/models/magic_field.rb
... ... @@ -1,54 +0,0 @@
1   -class MagicField < ActiveRecord::Base
2   - has_many :magic_field_relationships
3   - has_many :owners, :through => :magic_field_relationships, :as => :owner
4   - has_many :magic_options
5   - has_many :magic_attributes, :dependent => :destroy
6   -
7   - validates_presence_of :name, :datatype
8   - validates_format_of :name, :with => /\A[a-z][a-z0-9_]+\z/
9   -
10   - def type_cast(value)
11   - begin
12   - case datatype.to_sym
13   - when :check_box_boolean
14   - (value.to_int == 1) ? true : false
15   - when :date
16   - Date.parse(value)
17   - when :datetime
18   - Time.parse(value)
19   - when :integer
20   - value.to_int
21   - else
22   - value
23   - end
24   - rescue
25   - value
26   - end
27   - end
28   -
29   - # Display a nicer (possibly user-defined) name for the column or use a fancified default.
30   - def pretty_name
31   - super || name.humanize
32   - end
33   -
34   - #get or set a variable with the variable as the called method
35   - def self.method_missing(method, *args)
36   - debugger
37   -
38   - method_name = method.to_s
39   - super(method, *args)
40   - rescue NoMethodError
41   - debugger
42   - #set a value for a variable
43   - if method_name =~ /=$/
44   - var_name = method_name.gsub('=', '')
45   - value = args.first
46   - self[var_name] = value
47   - #retrieve a value
48   - else
49   - self[method_name]
50   - end
51   - end
52   -
53   -
54   -end
lib/has_magic_fields/models/magic_field_relationship.rb
... ... @@ -1,5 +0,0 @@
1   -class MagicFieldRelationship < ActiveRecord::Base
2   - belongs_to :magic_field
3   - belongs_to :owner, :polymorphic => true
4   - #belongs_to :extended_model, :polymorphic => true
5   -end
lib/has_magic_fields/models/magic_option.rb
... ... @@ -1,7 +0,0 @@
1   -class MagicOption < ActiveRecord::Base
2   - belongs_to :magic_field
3   -
4   - validates_presence_of :value
5   - validates_uniqueness_of :value, :scope => :magic_field_id
6   - validates_uniqueness_of :synonym, :scope => :magic_field_id, :if => Proc.new{|this| !this.synonym.nil? and !this.synonym.empty?}
7   -end
8 0 \ No newline at end of file
spec/has_magic_fields/magic_fileds_spec.rb
... ... @@ -100,6 +100,14 @@ describe HasMagicFields do
100 100 @bob = User.create(name:"bob", account: @account )
101 101 @bob.magic_fields.create(:name => 'birthday')
102 102 expect(lambda{@alice.birthday}).not_to raise_error
  103 + @bob.birthday = "2014-07-29"
  104 + expect(@bob.save).to be(true)
  105 + expect(@account.birthday).to be(nil)
  106 + expect(@alice.birthday).to be(nil)
  107 + @alice.birthday = "2013-07-29"
  108 + expect(@alice.save).to be(true)
  109 + expect(@alice.birthday).not_to eq(@bob.birthday)
103 110 end
  111 +
104 112 end
105 113 end
... ...
spec/spec_helper.rb
  1 +
  2 +require 'debugger'
  3 +
1 4 require 'rubygems'
  5 +require 'rspec/autorun'
2 6 require "active_record"
3 7 require 'active_support'
4 8 require 'sqlite3'
5   -require 'active_record/fixtures'
6   -require 'debugger'
7   -
8 9  
9 10 $LOAD_PATH.unshift(File.dirname(__FILE__))
10 11 $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'rails'))
... ... @@ -12,7 +13,13 @@ require &quot;init&quot;
12 13  
13 14 require "rails/railtie"
14 15  
15   -# ActiveRecord::Base.logger = Logger.new(STDOUT)
  16 +module Rails
  17 + def self.env
  18 + @_env ||= ActiveSupport::StringInquirer.new(ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development")
  19 + end
  20 +end
  21 +
  22 +
16 23 ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
17 24 # ActiveRecord::Base.configurations = true
18 25  
... ...