diff --git a/app/controllers/favorite_groups_controller.rb b/app/controllers/favorite_groups_controller.rb index 3f3b02f9e..f221e97e5 100644 --- a/app/controllers/favorite_groups_controller.rb +++ b/app/controllers/favorite_groups_controller.rb @@ -19,7 +19,7 @@ class FavoriteGroupsController < ApplicationController end def new - @favorite_group = authorize FavoriteGroup.new + @favorite_group = authorize FavoriteGroup.new(creator: CurrentUser.user) respond_with(@favorite_group) end diff --git a/app/models/favorite_group.rb b/app/models/favorite_group.rb index 2cbaacc05..eedde972d 100644 --- a/app/models/favorite_group.rb +++ b/app/models/favorite_group.rb @@ -10,6 +10,7 @@ class FavoriteGroup < ApplicationRecord validate :creator_can_create_favorite_groups, :on => :create validate :validate_number_of_posts validate :validate_posts + validate :validate_can_enable_privacy array_attribute :post_ids, parse: /\d+/, cast: :to_i @@ -83,6 +84,12 @@ class FavoriteGroup < ApplicationRecord end end + def validate_can_enable_privacy + if is_public_change == [true, false] && !Pundit.policy!(creator, self).can_enable_privacy? + errors.add(:base, "Can't enable privacy without a Gold account") + end + end + def self.normalize_name(name) name.gsub(/[[:space:]]+/, "_") end @@ -112,7 +119,7 @@ class FavoriteGroup < ApplicationRecord end def pretty_name - name.tr("_", " ") + name&.tr("_", " ") end def posts @@ -166,6 +173,18 @@ class FavoriteGroup < ApplicationRecord post_ids.include?(post_id) end + def is_private=(value) + self.is_public = !ActiveModel::Type::Boolean.new.cast(value) + end + + def is_private + !is_public? + end + + def is_private? + !is_public? + end + def self.available_includes [:creator] end diff --git a/app/policies/favorite_group_policy.rb b/app/policies/favorite_group_policy.rb index 05cefc298..8926a1f8b 100644 --- a/app/policies/favorite_group_policy.rb +++ b/app/policies/favorite_group_policy.rb @@ -15,7 +15,11 @@ class FavoriteGroupPolicy < ApplicationPolicy update? end + def can_enable_privacy? + record.creator.is_gold? + end + def permitted_attributes - [:name, :post_ids_string, :is_public, :post_ids, { post_ids: [] }] + [:name, :post_ids_string, :is_public, :is_private, :post_ids, { post_ids: [] }] end end diff --git a/app/views/favorite_groups/_form.html.erb b/app/views/favorite_groups/_form.html.erb new file mode 100644 index 000000000..ae4069218 --- /dev/null +++ b/app/views/favorite_groups/_form.html.erb @@ -0,0 +1,12 @@ +<%= edit_form_for(@favorite_group) do |f| %> + <%= f.input :name, as: :string, required: true, input_html: { value: @favorite_group.pretty_name } %> + <%= f.input :post_ids_string, label: "Posts", as: :text %> + <% if policy(@favorite_group).can_enable_privacy? %> + <%= f.input :is_private, label: "Private", as: :boolean, hint: "Don't allow others to view this favgroup." %> + <% elsif @favorite_group.is_private? %> + <%= f.input :is_private, label: "Private", as: :boolean, hint: "Don't allow others to view this favgroup. Warning: if you disable this, you can't re-enable it without ".html_safe + link_to("upgrading to Danbooru Gold", new_user_upgrade_path) + ". (".html_safe + link_to_wiki("learn more", "help:privacy_mode") + ")".html_safe %> + <% else %> + <%= f.input :is_private, label: "Private", as: :boolean, hint: link_to("Upgrade to Danbooru Gold to enable private favgroups", new_user_upgrade_path), input_html: { disabled: true } %> + <% end %> + <%= f.submit "Submit" %> +<% end %> diff --git a/app/views/favorite_groups/edit.html.erb b/app/views/favorite_groups/edit.html.erb index 29d7369eb..c74ea5149 100644 --- a/app/views/favorite_groups/edit.html.erb +++ b/app/views/favorite_groups/edit.html.erb @@ -2,12 +2,7 @@