[yast-devel] Webyast: ActiveSource

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: yast-devel+unsubscribe@opensuse.org For additional commands, e-mail: yast-devel+help@opensuse.org

Josef Reidinger write:
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?
Klaus currently say in validation task, that there should be existing solution. I find some and write some comments: act_without_database - http://agilewebdevelopment.com/plugins/acts_without_database It is pure hack, which is not nice and quite confusing ActiveModel : http://github.com/rails/rails/tree/master/activemodel That's exactly what I need. It is abstraction of ActiveResource and ActiveRecord functionality. I prefer to use it as base and add our own functionlity in some Base class (as ActiveModel is only mixins). Where I see problems is that it is not packaged, test-suite is not in good condition and I doesn't see much support if something goes wrong (maybe I am wrong, but it is not "officially" supported). -- Josef Reidinger YaST team maintainer of perl-Bootloader, YaST2-Repair, webyast modules language and time -- To unsubscribe, e-mail: yast-devel+unsubscribe@opensuse.org For additional commands, e-mail: yast-devel+help@opensuse.org
participants (1)
-
Josef Reidinger