Commit c595c46c04bc4f65cff4839449a60a590048e4a6
1 parent
e8562e57
Exists in
master
fix bug for is_required
Showing
2 changed files
with
24 additions
and
11 deletions
Show diff stats
lib/has_magic_fields/extend.rb
1 | 1 | module HasMagicFields |
2 | 2 | module Extend extend ActiveSupport::Concern |
3 | + include ActiveModel::Validations | |
3 | 4 | module ClassMethods |
4 | 5 | def has_magic_fields(options = {}) |
5 | 6 | # Associations |
... | ... | @@ -33,10 +34,8 @@ module HasMagicFields |
33 | 34 | def method_missing(method_id, *args) |
34 | 35 | super(method_id, *args) |
35 | 36 | rescue NoMethodError |
36 | - | |
37 | 37 | method_name = method_id.to_s |
38 | - attr_names = magic_fields.map(&:name) | |
39 | - super(method_id, *args) unless attr_names.include?(method_name) or (md = /[\?|\=]/.match(method_name) and attr_names.include?(md.pre_match)) | |
38 | + super(method_id, *args) unless magic_field_names.include?(method_name) or (md = /[\?|\=]/.match(method_name) and magic_field_names.include?(md.pre_match)) | |
40 | 39 | |
41 | 40 | if method_name =~ /=$/ |
42 | 41 | var_name = method_name.gsub('=', '') |
... | ... | @@ -47,6 +46,20 @@ module HasMagicFields |
47 | 46 | end |
48 | 47 | end |
49 | 48 | |
49 | + def magic_field_names | |
50 | + magic_fields.map(&:name) | |
51 | + end | |
52 | + | |
53 | + def valid?(context = nil) | |
54 | + output = super(context) | |
55 | + magic_fields.each do |field| | |
56 | + if field.is_required? | |
57 | + validates_presence_of(field.name) | |
58 | + end | |
59 | + end | |
60 | + errors.empty? && output | |
61 | + end | |
62 | + | |
50 | 63 | # Load the MagicAttribute(s) associated with attr_name and cast them to proper type. |
51 | 64 | def read_magic_attribute(field_name) |
52 | 65 | field = find_magic_field_by_name(field_name) |
... | ... | @@ -57,8 +70,8 @@ module HasMagicFields |
57 | 70 | |
58 | 71 | def write_magic_attribute(field_name, value) |
59 | 72 | field = find_magic_field_by_name(field_name) |
60 | - existing = find_magic_attribute_by_field(field) | |
61 | - (attr = existing.first) ? update_magic_attribute(attr, value) : create_magic_attribute(field, value) | |
73 | + attribute = find_magic_attribute_by_field(field) | |
74 | + (attr = attribute.first) ? update_magic_attribute(attr, value) : create_magic_attribute(field, value) | |
62 | 75 | end |
63 | 76 | |
64 | 77 | def find_magic_attribute_by_field(field) | ... | ... |
spec/has_magic_fields/magic_fileds_spec.rb
... | ... | @@ -21,18 +21,18 @@ describe HasMagicFields do |
21 | 21 | |
22 | 22 | it "allows setting and saving of magic attributes" do |
23 | 23 | @charlie.magic_fields.create(:name => 'salary') |
24 | - @charlie.name = "zhouzongsi" | |
25 | 24 | @charlie.salary = 50000 |
26 | 25 | @charlie.save |
27 | 26 | @charlie = Person.find(@charlie.id) |
28 | 27 | expect(@charlie.salary).not_to be(nil) |
29 | 28 | end |
30 | 29 | |
31 | - # it "forces required if is_required is true" do | |
32 | - # # TODO figure out why this fails | |
33 | - # @charlie.magic_fields.create(:name => "last_name", :is_required => true) | |
34 | - # expect(@charlie.save).to be(false) | |
35 | - # end | |
30 | + it "forces required if is_required is true" do | |
31 | + @charlie.magic_fields.create(:name => "last_name", :is_required => true) | |
32 | + expect(@charlie.save).to be(false) | |
33 | + @charlie.last_name = "zongsi" | |
34 | + expect(@charlie.save).to be(true) | |
35 | + end | |
36 | 36 | |
37 | 37 | it "allows datatype to be :date" do |
38 | 38 | @charlie.magic_fields.create(:name => "birthday", :datatype => :date) | ... | ... |