Commit c32bea850ca229f4d2b658872dc60f92fc4bfc09
1 parent
86cf5eaa
Exists in
master
add type_scoped to all fields
Showing
4 changed files
with
46 additions
and
33 deletions
Show diff stats
lib/app/models/magic_field_relationship.rb
@@ -2,13 +2,14 @@ class MagicFieldRelationship < ActiveRecord::Base | @@ -2,13 +2,14 @@ class MagicFieldRelationship < ActiveRecord::Base | ||
2 | belongs_to :magic_field | 2 | belongs_to :magic_field |
3 | belongs_to :owner, :polymorphic => true | 3 | belongs_to :owner, :polymorphic => true |
4 | #belongs_to :extended_model, :polymorphic => true | 4 | #belongs_to :extended_model, :polymorphic => true |
5 | - | ||
6 | - validates_uniqueness_of :name, scope: [:owner_id, :owner_type] | 5 | + validates_uniqueness_of :name, scope: [:owner_id, :owner_type, :type_scoped] |
6 | + validates_presence_of :name, :type_scoped | ||
7 | 7 | ||
8 | before_validation :sync_name | 8 | before_validation :sync_name |
9 | 9 | ||
10 | def sync_name | 10 | def sync_name |
11 | self.name = magic_field.name | 11 | self.name = magic_field.name |
12 | + self.type_scoped = magic_field.type_scoped.blank? ? self.owner_type : magic_field.type_scoped | ||
12 | end | 13 | end |
13 | 14 | ||
14 | end | 15 | end |
lib/generators/has_magic_fields/install/templates/migration.rb
@@ -8,6 +8,7 @@ class AddHasMagicFieldsTables < ActiveRecord::Migration | @@ -8,6 +8,7 @@ class AddHasMagicFieldsTables < ActiveRecord::Migration | ||
8 | t.column :is_required, :boolean, :default => false | 8 | t.column :is_required, :boolean, :default => false |
9 | t.column :include_blank, :boolean, :default => false | 9 | t.column :include_blank, :boolean, :default => false |
10 | t.column :allow_other, :boolean, :default => true | 10 | t.column :allow_other, :boolean, :default => true |
11 | + t.column :type_scoped, :string | ||
11 | t.column :created_at, :datetime | 12 | t.column :created_at, :datetime |
12 | t.column :updated_at, :datetime | 13 | t.column :updated_at, :datetime |
13 | end | 14 | end |
@@ -25,6 +26,7 @@ class AddHasMagicFieldsTables < ActiveRecord::Migration | @@ -25,6 +26,7 @@ class AddHasMagicFieldsTables < ActiveRecord::Migration | ||
25 | t.column :owner_id, :integer | 26 | t.column :owner_id, :integer |
26 | t.column :owner_type, :string | 27 | t.column :owner_type, :string |
27 | t.column :name, :string | 28 | t.column :name, :string |
29 | + t.column :type_scoped, :string | ||
28 | t.column :created_at, :datetime | 30 | t.column :created_at, :datetime |
29 | t.column :updated_at, :datetime | 31 | t.column :updated_at, :datetime |
30 | end | 32 | end |
@@ -39,7 +41,7 @@ class AddHasMagicFieldsTables < ActiveRecord::Migration | @@ -39,7 +41,7 @@ class AddHasMagicFieldsTables < ActiveRecord::Migration | ||
39 | 41 | ||
40 | add_index :magic_attribute_relationships, [:magic_attribute_id, :owner_id, :owner_type], name:"magic_attribute_id_owner", :unique => true | 42 | add_index :magic_attribute_relationships, [:magic_attribute_id, :owner_id, :owner_type], name:"magic_attribute_id_owner", :unique => true |
41 | add_index :magic_field_relationships, [:magic_field_id, :owner_id, :owner_type], name:"magic_field_id_owner", :unique => true | 43 | add_index :magic_field_relationships, [:magic_field_id, :owner_id, :owner_type], name:"magic_field_id_owner", :unique => true |
42 | - add_index :magic_field_relationships, [:name, :owner_id, :owner_type], name:"magic_field_name_owner", :unique => true | 44 | + add_index :magic_field_relationships, [:name, :type_scoped, :owner_id, :owner_type], name:"magic_field_name_owner", :unique => true |
43 | end | 45 | end |
44 | 46 | ||
45 | end | 47 | end |
46 | \ No newline at end of file | 48 | \ No newline at end of file |
lib/has_magic_fields/extend.rb
@@ -13,6 +13,7 @@ module HasMagicFields | @@ -13,6 +13,7 @@ module HasMagicFields | ||
13 | # Inheritence | 13 | # Inheritence |
14 | cattr_accessor :inherited_from | 14 | cattr_accessor :inherited_from |
15 | 15 | ||
16 | + | ||
16 | # if options[:through] is supplied, treat as an inherited relationship | 17 | # if options[:through] is supplied, treat as an inherited relationship |
17 | if self.inherited_from = options[:through] | 18 | if self.inherited_from = options[:through] |
18 | class_eval do | 19 | class_eval do |
@@ -27,12 +28,23 @@ module HasMagicFields | @@ -27,12 +28,23 @@ module HasMagicFields | ||
27 | else | 28 | else |
28 | has_many :magic_field_relationships, :as => :owner, :dependent => :destroy | 29 | has_many :magic_field_relationships, :as => :owner, :dependent => :destroy |
29 | has_many :magic_fields, :through => :magic_field_relationships, :dependent => :destroy | 30 | has_many :magic_fields, :through => :magic_field_relationships, :dependent => :destroy |
31 | + # alias_method_chain :magic_fields, :scoped | ||
30 | end | 32 | end |
31 | - | 33 | + alias_method :magic_fields_without_scoped, :magic_fields |
32 | end | 34 | end |
35 | + | ||
33 | end | 36 | end |
34 | 37 | ||
35 | included do | 38 | included do |
39 | + | ||
40 | + def create_magic_filed(options = {}) | ||
41 | + type_scoped = options[:type_scoped].blank? ? self.class.name : options[:type_scoped].classify | ||
42 | + self.magic_fields.create options.merge(type_scoped: type_scoped ) | ||
43 | + end | ||
44 | + | ||
45 | + def magic_fields_with_scoped | ||
46 | + magic_fields_without_scoped.where(type_scoped: self.class.name) | ||
47 | + end | ||
36 | 48 | ||
37 | def method_missing(method_id, *args) | 49 | def method_missing(method_id, *args) |
38 | super(method_id, *args) | 50 | super(method_id, *args) |
spec/has_magic_fields/magic_fileds_spec.rb
@@ -19,22 +19,22 @@ describe HasMagicFields do | @@ -19,22 +19,22 @@ describe HasMagicFields do | ||
19 | end | 19 | end |
20 | 20 | ||
21 | it "allows adding a magic field" do | 21 | it "allows adding a magic field" do |
22 | - @charlie.magic_fields.create(:name => 'salary') | ||
23 | - expect(@charlie.magic_fields.length).to be(1) | 22 | + @charlie.create_magic_filed(:name => 'salary') |
23 | + expect(@charlie.magic_fields.length).to eq(1) | ||
24 | end | 24 | end |
25 | 25 | ||
26 | it "validates_uniqueness_of name in a object" do | 26 | it "validates_uniqueness_of name in a object" do |
27 | - @charlie.magic_fields.create(:name => 'salary') | 27 | + @charlie.create_magic_filed(:name => 'salary') |
28 | before_fields_count = MagicField.count | 28 | before_fields_count = MagicField.count |
29 | - expect(@charlie.magic_fields.length).to be(1) | ||
30 | - expect(lambda{@charlie.magic_fields.create(:name => 'salary')}).to raise_error | ||
31 | - expect(@charlie.magic_fields.length).to be(1) | 29 | + expect(@charlie.magic_fields.length).to eq(1) |
30 | + expect(lambda{@charlie.create_magic_filed(:name => 'salary')}).to raise_error | ||
31 | + expect(@charlie.magic_fields.length).to eq(1) | ||
32 | after_fields_count = MagicField.count | 32 | after_fields_count = MagicField.count |
33 | expect(before_fields_count).to eq(after_fields_count) | 33 | expect(before_fields_count).to eq(after_fields_count) |
34 | end | 34 | end |
35 | 35 | ||
36 | it "allows setting and saving of magic attributes" do | 36 | it "allows setting and saving of magic attributes" do |
37 | - @charlie.magic_fields.create(:name => 'salary') | 37 | + @charlie.create_magic_filed(:name => 'salary') |
38 | @charlie.salary = 50000 | 38 | @charlie.salary = 50000 |
39 | @charlie.save | 39 | @charlie.save |
40 | @charlie = Person.find(@charlie.id) | 40 | @charlie = Person.find(@charlie.id) |
@@ -42,43 +42,43 @@ describe HasMagicFields do | @@ -42,43 +42,43 @@ describe HasMagicFields do | ||
42 | end | 42 | end |
43 | 43 | ||
44 | it "forces required if is_required is true" do | 44 | it "forces required if is_required is true" do |
45 | - @charlie.magic_fields.create(:name => "last_name", :is_required => true) | 45 | + @charlie.create_magic_filed(:name => "last_name", :is_required => true) |
46 | expect(@charlie.save).to be(false) | 46 | expect(@charlie.save).to be(false) |
47 | @charlie.last_name = "zongsi" | 47 | @charlie.last_name = "zongsi" |
48 | expect(@charlie.save).to be(true) | 48 | expect(@charlie.save).to be(true) |
49 | end | 49 | end |
50 | 50 | ||
51 | it "allows datatype to be :date" do | 51 | it "allows datatype to be :date" do |
52 | - @charlie.magic_fields.create(:name => "birthday", :datatype => :date) | 52 | + @charlie.create_magic_filed(:name => "birthday", :datatype => :date) |
53 | @charlie.birthday = Date.today | 53 | @charlie.birthday = Date.today |
54 | expect(@charlie.save).to be(true) | 54 | expect(@charlie.save).to be(true) |
55 | end | 55 | end |
56 | 56 | ||
57 | it "allows datatype to be :datetime" do | 57 | it "allows datatype to be :datetime" do |
58 | - @charlie.magic_fields.create(:name => "signed_up_at", :datatype => :datetime) | 58 | + @charlie.create_magic_filed(:name => "signed_up_at", :datatype => :datetime) |
59 | @charlie.signed_up_at = DateTime.now | 59 | @charlie.signed_up_at = DateTime.now |
60 | expect(@charlie.save).to be(true) | 60 | expect(@charlie.save).to be(true) |
61 | end | 61 | end |
62 | 62 | ||
63 | it "allows datatype to be :integer" do | 63 | it "allows datatype to be :integer" do |
64 | - @charlie.magic_fields.create(:name => "age", :datatype => :integer) | 64 | + @charlie.create_magic_filed(:name => "age", :datatype => :integer) |
65 | @charlie.age = 5 | 65 | @charlie.age = 5 |
66 | expect(@charlie.save).to be(true) | 66 | expect(@charlie.save).to be(true) |
67 | end | 67 | end |
68 | 68 | ||
69 | it "allows datatype to be :check_box_boolean" do | 69 | it "allows datatype to be :check_box_boolean" do |
70 | - @charlie.magic_fields.create(:name => "retired", :datatype => :check_box_boolean) | 70 | + @charlie.create_magic_filed(:name => "retired", :datatype => :check_box_boolean) |
71 | @charlie.retired = false | 71 | @charlie.retired = false |
72 | expect(@charlie.save).to be(true) | 72 | expect(@charlie.save).to be(true) |
73 | end | 73 | end |
74 | 74 | ||
75 | it "allows default to be set" do | 75 | it "allows default to be set" do |
76 | - @charlie.magic_fields.create(:name => "bonus", :default => "40000") | 76 | + @charlie.create_magic_filed(:name => "bonus", :default => "40000") |
77 | expect(@charlie.bonus).to eq("40000") | 77 | expect(@charlie.bonus).to eq("40000") |
78 | end | 78 | end |
79 | 79 | ||
80 | it "allows a pretty display name to be set" do | 80 | it "allows a pretty display name to be set" do |
81 | - @charlie.magic_fields.create(:name => "zip", :pretty_name => "Zip Code") | 81 | + @charlie.create_magic_filed(:name => "zip", :pretty_name => "Zip Code") |
82 | expect(@charlie.magic_fields.last.pretty_name).to eq("Zip Code") | 82 | expect(@charlie.magic_fields.last.pretty_name).to eq("Zip Code") |
83 | end | 83 | end |
84 | end | 84 | end |
@@ -107,19 +107,21 @@ describe HasMagicFields do | @@ -107,19 +107,21 @@ describe HasMagicFields do | ||
107 | end | 107 | end |
108 | 108 | ||
109 | it "allows adding a magic field to the child" do | 109 | it "allows adding a magic field to the child" do |
110 | - @alice.magic_fields.create(:name => 'salary') | 110 | + @alice.create_magic_filed(:name => 'salary') |
111 | + expect(@alice.magic_fields.length).to eq(1) | ||
111 | expect(lambda{@alice.salary}).not_to raise_error | 112 | expect(lambda{@alice.salary}).not_to raise_error |
113 | + | ||
112 | expect(lambda{@account.salary}).not_to raise_error | 114 | expect(lambda{@account.salary}).not_to raise_error |
113 | end | 115 | end |
114 | 116 | ||
115 | it "allows adding a magic field to the parent" do | 117 | it "allows adding a magic field to the parent" do |
116 | - @account.magic_fields.create(:name => 'age') | 118 | + @account.create_magic_filed(:name => 'age') |
117 | expect(lambda{@alice.age}).not_to raise_error | 119 | expect(lambda{@alice.age}).not_to raise_error |
118 | end | 120 | end |
119 | 121 | ||
120 | it "sets magic fields for all child models" do | 122 | it "sets magic fields for all child models" do |
121 | @bob = User.create(name:"bob", account: @account ) | 123 | @bob = User.create(name:"bob", account: @account ) |
122 | - @bob.magic_fields.create(:name => 'birthday') | 124 | + @bob.create_magic_filed(:name => 'birthday') |
123 | expect(lambda{@alice.birthday}).not_to raise_error | 125 | expect(lambda{@alice.birthday}).not_to raise_error |
124 | @bob.birthday = "2014-07-29" | 126 | @bob.birthday = "2014-07-29" |
125 | expect(@bob.save).to be(true) | 127 | expect(@bob.save).to be(true) |
@@ -132,22 +134,18 @@ describe HasMagicFields do | @@ -132,22 +134,18 @@ describe HasMagicFields do | ||
132 | 134 | ||
133 | 135 | ||
134 | it "validates_uniqueness_of name in all models object" do | 136 | it "validates_uniqueness_of name in all models object" do |
135 | - @alice.magic_fields.create(:name => 'salary') | 137 | + @alice.create_magic_filed(:name => 'salary') |
136 | before_fields_count = MagicField.count | 138 | before_fields_count = MagicField.count |
137 | - expect(@alice.magic_fields.length).to be(1) | ||
138 | - expect(lambda{@alice.magic_fields.create(:name => 'salary')}).to raise_error | ||
139 | - expect(@alice.magic_fields.length).to be(1) | ||
140 | - expect(before_fields_count).to eq(MagicField.count) | ||
141 | - | ||
142 | - expect(lambda{@account.magic_fields.create(:name => 'salary')}).to raise_error | 139 | + expect(@alice.magic_fields.length).to eq(1) |
140 | + expect(lambda{@alice.create_magic_filed(:name => 'salary')}).to raise_error | ||
141 | + expect(@alice.magic_fields.length).to eq(1) | ||
143 | expect(before_fields_count).to eq(MagicField.count) | 142 | expect(before_fields_count).to eq(MagicField.count) |
144 | 143 | ||
145 | @bob = User.create(name:"bob", account: @account ) | 144 | @bob = User.create(name:"bob", account: @account ) |
146 | - expect(lambda{@bob.magic_fields.create(:name => 'salary')}).to raise_error | ||
147 | - expect(before_fields_count).to eq(MagicField.count) | ||
148 | - | ||
149 | - # expect(lambda{@sample.magic_fields.create(:name => 'salary')}).not_to raise_error | ||
150 | - # expect(before_fields_count).to eq(MagicField.count - 1) | 145 | + expect(lambda{@bob.create_magic_filed(:name => 'salary')}).to raise_error |
146 | + expect(lambda{@sample.create_magic_filed(:name => 'salary')}).not_to raise_error | ||
147 | + expect(lambda{@account.create_magic_filed(:name => 'salary')}).not_to raise_error | ||
148 | + | ||
151 | end | 149 | end |
152 | end | 150 | end |
153 | end | 151 | end |