Hi,
I look on Friday on validations for our rest-service. I little play with code
and it is not so easy to correctly define validation which is called in model
context. I spend some time to google solution and there is
ActiveSupport::Callbacks, whichhas required functionality.
For correctly usage of Callbacks it is needed to call it on certain place in
model code. I also find that ActiveResource and ActiveRecord contain
interesting shortcuts and helpers, so I get idea that I should create general
ActiveSource, for such models. This ActiveSource provide all things which is
common for ActiveRecord and ActiveRecord such as save, find, create, load and
filters like after_save, before_validation and others. What developer need to
use this support is only provide method store (called from save, handle
storing to source - in our case DBus), load (read from source - also DBUS) and
optional initialize which should initialize by some defaults. It should also
contain some automatic to_xml and to_json (at least to_json in our code is not
DRY and to_xml should be also generated automatic).
So if developer use ActiveSource code looks like this.
class NtpSync < ActiveSource::Core
validates_inclusion_of :sync, [true,false]
before_filter :save { yapi_perm_check "ntp.synchronize" }
def load
@sync = false
end
def store
if @sync
raise NtpError.new(ret) unless YastService.Call
("YaPI::NTP::Synchronize")
end
return true #if false, then save also return false
end
end
and usage in controller:
NtpController < ApplicationController
before_filter :login_required
def show
ntp = Ntp.find
respond_to do |format|
format.html { render :xml => ntp.actions.to_xml(:root =>
:actions)}
#return xml only
format.xml { render :xml => ntp.actions.to_xml(:root =>
:actions)}
format.json { render :json => ntp.actions.to_json }
end
end
def update
root = params["ntp"]
if root == nil || root == {}
raise InvalidParameters.new :ntp => "Missing"
end
Ntp.create root
show
end
def create
update
end
end
What do you think about the idea? Any comments, suggestions? Should I start
with it?
--
Josef Reidinger
YaST team
maintainer of perl-Bootloader, YaST2-Repair, webyast modules language and
time
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]