2011-10-25 10 views
1

私のRails 3アプリでは、プロフィールのどの部分を他人が検索できるのかをユーザが指定できるようにしたい。ユーザー全体を見えなくするにはどうすればいいのか分かりますが、複数のフィールドを検索可能にするかどうかを別々に指定するにはどうしたらいいですか?プロフィールの部分を検索可能にする方法

さらに詳しい情報:

機能性の面では、私は、ユーザーが/settingsで検索可能にする選択したプロフィールのどの部分に基づいて検索を制限したいです。プロファイルの一部は、たとえば@user.profile.hometownまたは@user.profile.current_cityです。私はRailscasts #52Trevor Turk's tutorialの組み合わせを使って、他の人が設定のチェックボックスを使って検索できるものを設定しています。

設定で検索可能性が定義されている場合、ユーザーが/usersインデックスを検索(またはフィルタリング)すると、非表示になっているものは公開され検索可能になります。テーブルの列やグループを隠している限りDBでどのように動作するかについては、テーブルを非表示にすることを考えましたが、それは最適な解決策ではないかもしれません。私はできるだけ初心者であり、本当にそれについて正直であるとは思っていませんでした。

+0

情報は、質問に追加!私は今私のコメントを削除します。 – tvalent2

答えて

4

方法1 - 表示/非表示特定の列

だから、最も直接的な方法(とあなたが/非表示を見せたいもののほんの一握りがある場合、これは動作しますが)、単にブール列を作成することですあなたが表示/非表示にする必要があるすべてのために。したがって、電話番号フィールドを持っていれば、 "show_phone_number"という列を持つことができ、真の場合はそれを表示します。

方法2 - あなたが必要な場合があります表示/非表示セクション全体

次のレベルは、むしろ特定の列を表示/非表示のではなく、持っているブール列にそれぞれのためなどshow_contact_infoのようなもの、show_photosを、隠す/あなたのショーユーザーが表示または非表示にする論理セクション。

次に、あなたのビューで、あなたのようなものがあるだろう:

app/views/user/show.html.erb(または.hamlまたは使用しているものは何でも)を

.... 
<% if @user.show_contact_info %> 
    <%= render :partial => "user_contact_info", :locals => {:user => @user} %> 
<% end %> 

app/views/partials/_user_contact_info.html.erb

<%=h user.email %><br /> 
<%=h user.phone_number %><br /> 
<%= user.blog_url %><br /> 
... 

方法3 - ショー誰がそれを見ているかに基づいてセクションを非表示/非表示にする

最後に(ここのコードはテストされていませんが、あなたはそのアイデアを得るでしょう)あなたのサイトは社会構造を持ち、他の人には情報を表示したくないとしましょう。基本的には何らかの形で次のように必要があります(どのようなセクションを表示することができます)

  • セクションの可視性
  • 役割(友人、フォロワー、パブリック、プライベート)
  • これらを作るためのいくつかのメソッドを

    class User < ActiveRecord::Base 
        has_many :friends, :through => "friendships" # or whatever construct you have 
        has_many :followers, :through => "followings" # or whatever construct you have 
        has_many :profile_visibilities 
        ... 
    
        def is_friends_with(user) 
        friends.include?(user) 
        end 
    
        def is_a_follower_of(user) 
        user.followers.include?(self) 
        end 
    
        def can_see(visibilities) 
        visibilities.each do |v| 
         v.user == self || v.is_public || can_see_because_we_are_friends(v) || can_see_because_i_follow(v) 
        end 
        end 
    
        private: 
        def can_see_because_we_are_friends(visibility) 
        visibility.is_friend && is_friends_with(visibility.user) 
        end 
    
        def can_see_because_i_follow(visibility) 
        visibility.is_follower && is_follower_of(visibility.user) 
        end 
    end 
    
    :だから

を理解するには、clear /簡単な関係は、あなたのUserモデルでは、あなたのようなものを持っていると思います

はその後ProfileVisibilitiesというクラス:

app/controllers/users_controller.rb

... 
def show 
    @user = User.find(params[:id]) 
    @contact_info_visibilities = ProfileVisibilities.find(:all, :conditions = ['user_id = ? AND profile_section = "contact_info"', @user.id] 
    @photo_visibilities = ProfileVisibilities.find(:all, :conditions = ['user_id = ? AND profile_section = "photos"', @user.id] 
    # ... and more for each section visibility you need 
end 
... 

そして中:次に、表のようなもの、あなたのコントローラに続いてprofile_visibilities

id | user_id | profile_section | visibility 
---------------------------------------------- 
1 | 1  | contact_info | public  # <= visible to everyone 
2 | 1  | personal_info | friends  # <= visible only to "friends" 
3 | 1  | blog_posts  | friends  # <= visible to "friends" 
4 | 1  | blog_posts  | followers  # <= ...and followers 
5 | 1  | photos   | friends  # <= visible only to "friends" 

class ProfileVisibilities < ActiveRecord::Base 
    belongs_to :user 
    ... 

    def is_public 
    visibility == "public" 
    end 

    def is_friend 
    visibility == "friends" 
    end 

    def is_follower 
    visibility == "followers" 

    def is_private 
    !is_public && !is_friend && !is_follower 
    end 
end 

と呼ばれますあなたのビュー:

app/views/user/show.html.erb

... 
<% if current_user.can_see(@contact_info_visibilities) %> 
    <%= render :partial => "user_contact_info", :locals => {:user => @user} 
<% end %> 

<% if current_user.can_see(@photo_visibilities) %> 
    <%= render :partial => "user_photos", :locals => {:user => @user} 
<% end %> 
... 
+0

うわー、消化するためにロード。どうもありがとうございました! – tvalent2

関連する問題