Skip to content

Commit

Permalink
Fixed KVO-related problems. And added proper tests for it.
Browse files Browse the repository at this point in the history
  • Loading branch information
clayallsopp committed Oct 27, 2012
1 parent 93e1519 commit 64e7fd5
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

- Fixed device crash when taking photo

- Fixed problems related to KVO-ing `Formable` objects.

## 1.1.2 - October 6, 2012

### Bug Fixes
Expand Down
10 changes: 10 additions & 0 deletions lib/formotion/model/formable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module ClassMethods
attr_accessor prop
end

# Does NOT get called when KVO occurs.
# (KVO uses isa swizzling and not proper subclassing)
def inherited(subclass)
INHERITABLE_ATTRIBUTES.each do |inheritable_attribute|
instance_var = "@#{inheritable_attribute}"
Expand All @@ -17,6 +19,7 @@ def inherited(subclass)
end

def form_properties
@form_properties ||= self.superclass.form_properties if is_kvo_subclass?
@form_properties ||= []
end

Expand All @@ -38,9 +41,16 @@ def form_property(property, row_type, options = {})
# EX
# form_title "Some Settings"
def form_title(title = -1)
@form_title ||= self.superclass.form_title if is_kvo_subclass?
@form_title = title if title != -1
@form_title
end

private
# Terrible, terrible hack.
def is_kvo_subclass?
self.to_s =~ /^NSKVONotifying_/
end
end

# Creates a Formotion::Form out of the model
Expand Down
45 changes: 45 additions & 0 deletions spec/formable/kvo_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class User
include Formotion::Formable

attr_accessor :name, :score, :team

form_property :name, :string
form_property :score, :number, transform: lambda { |value| value.to_i }

form_property :team, :picker, items: ["Red", "Blue", "Green"]

form_title "Edit User"

def initialize(name, score, team)
self.name = name
self.score = score
self.team = team
end
end

class ObserverTest
include BubbleWrap::KVO

def users
@users ||= [User.new("Harry", 100, "Green"),
User.new("Ron", 80, "Blue"),
User.new("Hermione", 120, "Red")]
end

def start_observing
observe(self.users.first, "team") do |old_value, new_value|
end
end
end

describe "Formotion::Formable w/ KVO" do
it "should work" do
test = ObserverTest.new
test.start_observing

test.users[0].to_form.title.should == test.users[1].to_form.title
test.users[0].class.form_properties.should == test.users[1].class.form_properties

test.unobserve_all
end
end
31 changes: 31 additions & 0 deletions spec/formable/subclass_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class User
include Formotion::Formable

attr_accessor :name, :score, :team

form_property :name, :string
form_property :score, :number, transform: lambda { |value| value.to_i }

form_property :team, :picker, items: ["Red", "Blue", "Green"]

form_title "Edit User"

def initialize(name, score, team)
self.name = name
self.score = score
self.team = team
end
end

class AwesomeUser < User
end

describe "Formotion::Formable w/ Subclasses" do
it "should work" do
user = User.new("Harry", 100, "Green")
awesome = AwesomeUser.new("Clay", 200, "Red")

user.to_form.title.should == awesome.to_form.title
user.class.form_properties.should == awesome.class.form_properties
end
end

0 comments on commit 64e7fd5

Please sign in to comment.