So I have some data that gets pulled from another rails app in a controller lets call it ExampleController and I want to validate it as being there in my model before allowi
I maybe have misunderstood the question since my answer is simple. However here's a solution that doesn't resort to metaprogramming, but to the fact that Wizard (the class not objects it creates ) is a singleton/constant.
class ExampleController < ApplicationController
def valid_data?
data = #data could be nil or not
result = data.blank?
Awizard.valid_data= result
result
end
end
class Wizard
cattr_accessor :valid_data
def valid_data?
self.class.valid_data
end
end
If course ExampleController#valid_data must have been called before you play around with a Wizard passing step_one.
(raised by @Valery Kvon)
The argument is that Wizard is global to the application and that @wizard instances will be dependant on a global state and are therefore badly encapsulated. But Data, coming from another site, is gloabl in the scope of your app. So there's no mismatch with Wizard beeing the one holding the data. On the contrary it can be considered as a feature.
One example. Wizards magic is only efficient at full moon. Application SkyReport sends data :
:full_moon => true
It affects all wizards in stage 1 if they need to go on step2 of their power. Therefore relying on the global state of Wizard.valid_data?
is exactly what we want...
However if each wizard has a personal message coming from Gandalf's application, then we'll want to inforce the invocation of Gandalf's data but then the solution is even simpler :
# in example_controller.rb
before_filter :set_wizard_data, :only => [:create, :update]
....
def set_wizard_data
@wizard = Wizard.find params[:id]
@wizard.valid_data= valid_data
end
But this again implies that Gandalf.app knows (something of) the @wizard and from how the problem is presented, data coming from the other site is pretty agnostic !
The issue here is that we don't know enough about the app, its requirements and underlying logic to decide what's good or not...