Responses と Posts、Calls & Meetings の間にポリモーフィックな関係があります。
class Response < ActiveRecord::Base
belongs_to :responseable, polymorphic: true
...
end
class Post < ActiveRecord::Base
has_many :responses, as: :responseable, dependent: :destroy
...
end
class Call < ActiveRecord::Base
has_many :responses, as: :responseable, dependent: :destroy
...
end
class Meeting < ActiveRecord::Base
has_many :responses, as: :responseable, dependent: :destroy
...
end
CanCan を使用して自分の能力を定義しています。ネストされたリソース機能を使用して、親の能力に応じて応答の表示を承認しています。
class ResponsesController < ApplicationController
before_filter :authorize_parent
load_resource :post
load_resource :call
load_resource :meeting
load_and_authorize_resource :response, :through => [:post, :call, :meeting]
...
private
def authorize_parent
authorize! :read, (@post || @call || @meeting)
end
end
すべての機能は、コントローラーの標準アクションを使用して正常に機能しています。
ただし、Responses コントローラーには、15 秒ごとに JS スクリプトによって新しい応答をポーリングするために使用されるアクションがあります。
def polling
current_user_id = params[:current_user_id]
responseable_type = params[:responseable_type]
klass = [Post, Call, Meeting].detect { |c| responseable_type == c.name }
@responseable = klass.find(params[:responseable_id])
undivided_millisecond_epoch_time_in_integer = params[:after]
undivided_millisecond_epoch_time_in_decimal = (undivided_millisecond_epoch_time_in_integer).to_d
divided_millisecond_epoch_time_in_decimal = (undivided_millisecond_epoch_time_in_decimal / 1000000).to_d
@responses = @responseable.responses.where("created_at > ? AND user_id <> ?", Time.at(divided_millisecond_epoch_time_in_decimal), current_user_id)
end
何を試しても、これを機能させることはできません。つまり、「このページにアクセスする権限がありません」という応答が返されます。このカスタム アクションを機能させるには、何かを追加する必要があると思いますが、どこで何をするかについては少し迷っています。
どんな助けでも大歓迎です。
編集:私の能力ファイルに詳細を追加しました:
if user.role == "client"
can :index, Post, :user_expert_private => false
can :index, Post, :user_expert_private => true, :user_id => user.id
can :show, Post, :user_expert_private => false, :countries => { :id => user.country_ids}
can :show, Post, :user_expert_private => true, :user_id => user.id
can :create, Post
can :edit, Post, :user_id => user.id
can :update, Post, :user_id => user.id
can :read, Response
can :create, Response
can :polling, Response
can :read, Call, :user_id => user.id
can :create, Call, :user_id => user.id
can :edit, Call, :user_id => user.id
can :update, Call, :user_id => user.id
can :read, Meeting, :user_id => user.id
can :create, Meeting, :user_id => user.id
can :edit, Meeting, :user_id => user.id
can :update, Meeting, :user_id => user.id
can :responses, :polling
can :posts, :autocomplete
end