Added option to make favorite groups public
This commit is contained in:
@@ -11,7 +11,7 @@ class FavoriteGroup < ApplicationRecord
|
|||||||
validate :creator_can_create_favorite_groups, :on => :create
|
validate :creator_can_create_favorite_groups, :on => :create
|
||||||
validate :validate_number_of_posts
|
validate :validate_number_of_posts
|
||||||
before_save :update_post_count
|
before_save :update_post_count
|
||||||
attr_accessible :name, :post_ids, :post_id_array, :as => [:member, :gold, :platinum, :builder, :moderator, :admin, :default]
|
attr_accessible :name, :post_ids, :post_id_array, :is_public, :as => [:member, :gold, :platinum, :builder, :moderator, :admin, :default]
|
||||||
|
|
||||||
module SearchMethods
|
module SearchMethods
|
||||||
def for_creator(user_id)
|
def for_creator(user_id)
|
||||||
@@ -33,27 +33,30 @@ class FavoriteGroup < ApplicationRecord
|
|||||||
where("name ilike ? escape E'\\\\'", name.to_escaped_for_sql_like)
|
where("name ilike ? escape E'\\\\'", name.to_escaped_for_sql_like)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def hide_private(user,params)
|
||||||
|
if user.hide_favorites?
|
||||||
|
where("is_public = true")
|
||||||
|
elsif params[:is_public].present?
|
||||||
|
where("is_public = ?", params[:is_public])
|
||||||
|
else
|
||||||
|
where("true")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def search(params)
|
def search(params)
|
||||||
q = super
|
q = super
|
||||||
params = {} if params.blank?
|
params = {} if params.blank?
|
||||||
|
|
||||||
if params[:creator_id].present?
|
if params[:creator_id].present?
|
||||||
user = User.find(params[:creator_id])
|
user = User.find(params[:creator_id])
|
||||||
|
q = q.hide_private(user,params)
|
||||||
if user.hide_favorites?
|
|
||||||
raise User::PrivilegeError.new
|
|
||||||
end
|
|
||||||
|
|
||||||
q = q.where("creator_id = ?", user.id)
|
q = q.where("creator_id = ?", user.id)
|
||||||
elsif params[:creator_name].present?
|
elsif params[:creator_name].present?
|
||||||
user = User.find_by_name(params[:creator_name])
|
user = User.find_by_name(params[:creator_name])
|
||||||
|
q = q.hide_private(user,params)
|
||||||
if user.hide_favorites?
|
|
||||||
raise User::PrivilegeError.new
|
|
||||||
end
|
|
||||||
|
|
||||||
q = q.where("creator_id = ?", user.id)
|
q = q.where("creator_id = ?", user.id)
|
||||||
else
|
else
|
||||||
|
q = q.hide_private(CurrentUser.user,params)
|
||||||
q = q.where("creator_id = ?", CurrentUser.user.id)
|
q = q.where("creator_id = ?", CurrentUser.user.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -243,6 +246,6 @@ class FavoriteGroup < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def viewable_by?(user)
|
def viewable_by?(user)
|
||||||
creator_id == user.id || !creator.hide_favorites?
|
creator_id == user.id || !creator.hide_favorites? || is_public
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
<%= simple_form_for(@favorite_group) do |f| %>
|
<%= simple_form_for(@favorite_group) do |f| %>
|
||||||
<%= f.input :name, :as => :string, :input_html => { :value => @favorite_group.pretty_name } %>
|
<%= f.input :name, :as => :string, :input_html => { :value => @favorite_group.pretty_name } %>
|
||||||
<%= f.input :post_ids, :label => "Posts" %>
|
<%= f.input :post_ids, :label => "Posts" %>
|
||||||
|
<%= f.input :is_public %>
|
||||||
<%= f.button :submit, "Submit" %>
|
<%= f.button :submit, "Submit" %>
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
class AddIsPublicToFavoriteGroups < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
FavoriteGroup.without_timeout do
|
||||||
|
add_column :favorite_groups, :is_public, :boolean, default: false, null: false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1044,7 +1044,8 @@ CREATE TABLE favorite_groups (
|
|||||||
post_ids text DEFAULT ''::text NOT NULL,
|
post_ids text DEFAULT ''::text NOT NULL,
|
||||||
post_count integer DEFAULT 0 NOT NULL,
|
post_count integer DEFAULT 0 NOT NULL,
|
||||||
created_at timestamp without time zone,
|
created_at timestamp without time zone,
|
||||||
updated_at timestamp without time zone
|
updated_at timestamp without time zone,
|
||||||
|
is_public boolean DEFAULT false NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
@@ -7530,3 +7531,5 @@ INSERT INTO schema_migrations (version) VALUES ('20171106075030');
|
|||||||
|
|
||||||
INSERT INTO schema_migrations (version) VALUES ('20171127195124');
|
INSERT INTO schema_migrations (version) VALUES ('20171127195124');
|
||||||
|
|
||||||
|
INSERT INTO schema_migrations (version) VALUES ('20171219001521');
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user