Ruby on rails 3 如何使配置文件的某些部分可搜索或不可搜索

Ruby on rails 3 如何使配置文件的某些部分可搜索或不可搜索,ruby-on-rails-3,Ruby On Rails 3,在我的Rails 3应用程序中,我希望允许用户指定其他人可以搜索其个人资料的哪些部分。如果我想让整个用户看不见,我知道如何做到这一点,但我如何设置它,使多个字段可以指定为可搜索或不可单独搜索 更多信息: 在功能方面,我想根据用户选择在/settings中搜索其个人资料的哪些部分来限制搜索。配置文件的部分内容可能是,例如,@user.profile.homely或@user.profile.current_city。我正在使用和的组合来设置其他人可以通过设置中的复选框搜索的内容 如果在“设置”中定

在我的Rails 3应用程序中,我希望允许用户指定其他人可以搜索其个人资料的哪些部分。如果我想让整个用户看不见,我知道如何做到这一点,但我如何设置它,使多个字段可以指定为可搜索或不可单独搜索

更多信息:

在功能方面,我想根据用户选择在/settings中搜索其个人资料的哪些部分来限制搜索。配置文件的部分内容可能是,例如,@user.profile.homely或@user.profile.current_city。我正在使用和的组合来设置其他人可以通过设置中的复选框搜索的内容

如果在“设置”中定义了可搜索性,则当用户搜索或筛选/users索引时,未隐藏的内容将是公共的,并且可搜索。关于在DB中隐藏表列或分组的工作方式,我考虑过隐藏表,但这可能不是最好的解决方案。老实说,我是一个初学者,并没有认真考虑过这个问题。

方法1-显示/隐藏特定列 因此,如果您只想显示/隐藏少数内容,那么最直接的方法就是为需要显示/隐藏的所有内容创建一个布尔列。所以,如果您有一个电话号码字段,您可以有一个名为show_phone_number的列,如果为true,它将显示它

方法2-显示/隐藏整个部分 您可能需要的下一个级别不是显示/隐藏特定列,而是为用户显示或隐藏的每个逻辑部分显示/隐藏布尔列,例如显示\联系\信息、显示\照片等

那么在你看来,你会有这样的想法:

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
app/views/user/show.html.erb或.haml或您正在使用的任何内容

....
<% if @user.show_contact_info %>
  <%= render :partial => "user_contact_info", :locals => {:user => @user} %>
<% end %>
方法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
然后是一个名为profile_visibilies的表

在你看来:

app/views/user/show.html.erb


信息添加到问题中!我现在要删除我的评论。哇,要消化的东西太多了。非常感谢!
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
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"
...
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
...
...
<% 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 %>
...