내가 가진 정의 간단한 다형성 코멘트 모델은 다음과 같습니다 당신은 내가의 숫자를 생성하기 위해 약간의 레디 스의 마법을 사용하여 볼 수 있습니다어떻게 모델의 URL을 리팩토링
class Comment < ActiveRecord::Base
include Rails.application.routes.url_helpers
if Rails.env.production?
default_url_options[:host] = "www.livesite.org"
else
default_url_options[:host] = "livesite.dev"
end
attr_accessible :content
attr_accessible :commentable_id, :commentable_type
belongs_to :commentable, polymorphic: true
belongs_to :user
validates_presence_of :content
after_create :subscribe_to, :notify_subscribers
private
def subscribe_to
commentable.rdb[:subscribers].sadd user_id
end
def notify_subscribers
subscriber_ids = commentable.rdb[:subscribers].smembers.to_a
subscriber_ids.delete user_id.to_s
# remove the author's id from the array
subscribers = User.where(id: subscriber_ids)
subscribers.each do |subscriber|
subscriber.notifications.create(
content: "<a href='#{ user_url(user) }'>#{user.name}</a> commented about <a href='#{ polymorphic_url(commentable) }'>#{commentable.name}</a>",
read: false,
notifyable: commentable
)
end
end
end
특정 commentable에 가입자지만, 내 질문은 어떻게 여기에 polymorphic_url
부분을 추상화 할 수있는 모델입니다. 그것은 모델 수준에서 가지고있는 것이 이상하게 보입니다. 더 나은 접근 방법이 있습니까? 여기에있는 것은 url_helpers
을 포함해야하며 Capybara와 협력하여 실제 palava를 테스트한다는 의미입니다. 참고로
는 Notification.rb는 다음과 같다 :
class Notification < ActiveRecord::Base
attr_accessible :subject, :read, :user_id, :notifyable
belongs_to :user
belongs_to :notifyable, polymorphic: true
default_scope order('created_at DESC')
end
서비스 객체로 이동하는 것은 좋은 생각입니다. 그러나이 작업을 시작했습니다. 그러나 서비스 객체에서'polymorphic_url'을 사용하지 않는다는 것을 어떻게 볼 수 있습니까? 이것은 내가 Capybara와 Selenium에서 여전히 같은 문제를 겪고 있음을 의미합니다. 테스트에서 실행중인 전체 도메인/포트를 항상 알 수는 없습니다. 어떤 충고? – idrysdale
아, PinnyM이 말한 것을해야합니다. – DiegoSalazar
Capybara가'capybara.server_port = 7787'을 사용하여 실행중인 포트를 지정하는 것을 끝내기 시작했습니다.이 포트는 test.rb에서 사용되었습니다. config.action_mailer.default_url_options = {: host => "127.0.0.1:7787"}' – idrysdale