Singular ResourcesにActiveResourceでキレイな接続に失敗
Rails 2.3.2
Singular Resourcesでrouteを設定した情報にActiveResourceでキレイに接続することができないー。。。
とりあえずActiveResource::Baseの
find(:one, :from => '/hoge.xml', :params => {})
を利用してつながったけど、もっとキレイな方法はないかなー。。。ActiveResourceはSingular ResourcesでできるURLには対応してないのかな。。。
一応試した内容
server側となるWebアプリのroutes.rb
ActionController::Routing::Routes.draw do |map| map.resource :authentication, :only => [:show] : : end
関係はないけど、Controller
class AuthenticationsController < ApplicationController # GET /authenticate # GET /authenticate.xml def show @authentication = Authentication.new user = User.authenticate(params[:account], params[:password]) @authentication.result = (not user.nil?) @authentication.user_id = user.id if not user.nil? respond_to do |format| format.html # show.html.erb format.xml { render :xml => @authentication } end end end
client側となるWebアプリのActiveResourceを使ったModel
class Authentication < ActiveResource::Base self.site = 'http://localhost:3000/' def self.authenticate(account, password) find(:one, :from => '/authentication.xml', :params => { :account => account, :password => password }) end end
無理やりSingular Resourcesを使ってるので、どうせキレイにActiveResourceで利用できないなら、Singular Resourcesを使うのをやめようかなー。
あと、ActiveRecordでもActiveResourceでもないModelを作りたいんだけど、どうやって作るのが良いのかもわからないー。。。to_xmlとかのメソッドがほしいよー。。。無理やりhashを持たせて下のような感じで作った。
class Authentication class << self def root # ActiveResource::Base の element_name のデフォルト値を真似した to_s.split("::").last.underscore end end attr_accessor :attributes def initialize @attributes = {} result = false user_id = nil end def result attributes[:result] end def result=(value) @attributes[:result] = value end def user_id attributes[:user_id] end def user_id=(value) @attributes[:user_id] = value end def to_xml(options={}) # ActiveResource::Base の to_xml を真似した attributes.to_xml({:root => self.class.root}.merge(options)) end end