How to validate in a model, data from a controller

亡梦爱人 提交于 2019-12-01 18:25:14

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.

UPDATE:Reasoning about the global state problem

(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...

The only way to share controller level data with model is through external accessor. Using metaprogramming you can trick the way to pass it to a model instance.

controller

def valid_data?            
  data = #data could be nil or not
  result = data.blank? ? false : true
  instance_eval <<-EOV
    def AWizard.new(*args)
      super(*args).tap {|aw| aw.external_valid = #{result}}
    end
  EOV
  result
end

model

class AWizard
  attr_accessor :external_valid

  def initialize(attributes = {})
    attributes.each do |name, value|
      send("#{name}=", value)
    end
  end

  validate :first_step_data, :if => lambda { |o| o.current_step == "step1" };

  def first_step_data
    # :external_valid would be true or false according to a valid_data?. Nil would be if valid_data? has not been called
    if external_valid == false
      errors.add ...
    end
  end
end

You can pass the data from the controller as a parameter to the validation method in the model.

In your models/awizard.rb

def valid_for_step_one?(some_external_data)
  #validation logic here
end

So that in your controller, you can can call:

model.valid_for_step_one?(data_from_controller)

It would also be good if you can give a description of the data you are getting from the controller. How is it related to the model awizard?

Because another option is to set the external data as an attribute of the model. Then you can use it in your validation functions from there.

So I tried to edit and add to the @charlysisto question as this was closest to the answer but it did not work so here is the solution I used, as suggested the answer was to send the data from the controller to the model (Although the answers left out using the view to call the controller method) here is my solution

Model - models/awizard.rb

class Awizard
  include ActiveModel::Validations

  cattr_accessor :valid_data

  validate :data_validation :if => lambda { |o| o.current_step == "step1" }

  def data_validation
    if self.valid_data == false || self.valid_data.blank?
      errors.add(:valid_data, "not found")
    end
  end

  #Other wizard stuff

end

View - awizard/_step1.html.erb

<div class="field">
  <% f.label "valid data? %>
  <% @_controller.valid_data %> #Call controller method to send data to model
</div>

Controller

class AwizardController < ApplicationController

  def valid_data
    data = #data from elsewhere
    if !data.blank?
      Awizard.valid_data = true
    else
      Awizard.valid_data = false
  end

end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!