From 770d23ce94f053e549797794fe607076646e5b63 Mon Sep 17 00:00:00 2001 From: Aidan Cornelius-Bell Date: Tue, 10 Dec 2024 09:53:13 +1030 Subject: [PATCH] added ability to hide AI summaries on user model --- .../users/registrations_controller.rb | 20 +++++++-- app/models/user.rb | 44 ++++++++++--------- app/views/devise/registrations/edit.html.erb | 12 +++++ app/views/pubview/index.html.erb | 2 +- app/views/user_manager/edit.html.erb | 20 ++++----- ...41209215122_add_ai_preferences_to_users.rb | 5 +++ db/schema.rb | 3 +- 7 files changed, 70 insertions(+), 36 deletions(-) create mode 100644 db/migrate/20241209215122_add_ai_preferences_to_users.rb diff --git a/app/controllers/users/registrations_controller.rb b/app/controllers/users/registrations_controller.rb index 5a12454..afea816 100644 --- a/app/controllers/users/registrations_controller.rb +++ b/app/controllers/users/registrations_controller.rb @@ -14,12 +14,24 @@ class Users::RegistrationsController < Devise::RegistrationsController private def sign_up_params - params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :subscribe_to_newsletter, :payment_amount, :support_type) + params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :subscribe_to_newsletter, :payment_amount, :support_type, :hide_ai_summaries) + end + + def account_update_params + params.require(:user).permit( + :first_name, + :last_name, + :email, + :password, + :password_confirmation, + :current_password, + :hide_ai_summaries + ) end def process_payment(user) amount = (params[:user][:payment_amount].to_f * 100).to_i # Convert to cents - + begin customer = Stripe::Customer.create({ email: user.email, @@ -32,7 +44,7 @@ class Users::RegistrationsController < Devise::RegistrationsController subscription = Stripe::Subscription.create({ customer: customer.id, items: [{ - price_data: { + price_data: { unit_amount: amount, currency: 'usd', recurring: { interval: 'year' }, @@ -70,4 +82,4 @@ class Users::RegistrationsController < Devise::RegistrationsController respond_with_navigational(resource) { render :new } end end -end \ No newline at end of file +end diff --git a/app/models/user.rb b/app/models/user.rb index f5a2f92..d1ac8e0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -5,13 +5,13 @@ class User < ApplicationRecord :recoverable, :rememberable, :validatable, :confirmable, :lockable, :two_factor_authenticatable, :two_factor_backupable, otp_secret_encryption_key: Rails.application.credentials.active_record_encryption[:primary_key] - + encrypts :otp_secret attr_accessor :otp_plain_secret - + validates :first_name, presence: true validates :last_name, presence: true - + attribute :subscribe_to_newsletter, :boolean, default: false attribute :stripe_customer_id, :string attribute :subscription_status, :string @@ -20,50 +20,54 @@ class User < ApplicationRecord attribute :payment_amount, :decimal attribute :buttondown_status, :string, default: 'unactivated' attribute :support_type, :string - + def one_time_donor? subscription_status == 'one_time' end - + def ongoing_subscriber? subscription_status == 'active' end - + def non_financial_supporter? support_type == 'non_financial' end - + def full_name "#{first_name} #{last_name}" end - + def admin? admin end - + + def show_ai_summaries? + !hide_ai_summaries + end + def paid_user? ['premium', 'gifted', 'trialed', 'churning', 'paused', 'past_due'].include?(buttondown_status) || subscription_status == 'active' || subscription_status == 'one_time' end - + def active_subscriber? ['premium', 'trialed', 'gifted'].include?(buttondown_status) end - + def post_confirmation_setup sync_with_buttondown if subscribe_to_newsletter? send_welcome_email end - + def generate_two_factor_secret_if_missing! return unless otp_secret.nil? update!(otp_secret: User.generate_otp_secret) end - + def enable_two_factor! update!(otp_required_for_login: true) end - + def disable_two_factor! update!( otp_required_for_login: false, @@ -71,13 +75,13 @@ class User < ApplicationRecord otp_backup_codes: nil ) end - + def two_factor_qr_code_uri issuer = 'Your App Name' label = "#{issuer}:#{email}" otp_provisioning_uri(label, issuer: issuer) end - + def generate_otp_backup_codes! codes = [] 10.times do @@ -85,22 +89,22 @@ class User < ApplicationRecord end update!(otp_backup_codes: codes) end - + # Getter and setter for otp_backup_codes def otp_backup_codes return [] if super.nil? JSON.parse(super) end - + def otp_backup_codes=(codes) super(codes.to_json) end - + private def sync_with_buttondown ButtondownService.new.subscribe(self) end - + def send_welcome_email WelcomeMailer.welcome_email(self).deliver_later end diff --git a/app/views/devise/registrations/edit.html.erb b/app/views/devise/registrations/edit.html.erb index 95a875f..df6c500 100644 --- a/app/views/devise/registrations/edit.html.erb +++ b/app/views/devise/registrations/edit.html.erb @@ -55,6 +55,18 @@ +
+ +
+ <%= f.label :hide_ai_summaries do %> + <%= f.check_box :hide_ai_summaries %> + Hide AI-generated summaries + <% end %> +

When enabled, AI-generated summaries will not be displayed on posts.

+
+ +
+
<%= f.label :current_password %> (we need your current password to confirm your changes) <%= f.password_field :current_password, autocomplete: "current-password" %> diff --git a/app/views/pubview/index.html.erb b/app/views/pubview/index.html.erb index f77ccfe..ad9d30c 100644 --- a/app/views/pubview/index.html.erb +++ b/app/views/pubview/index.html.erb @@ -38,7 +38,7 @@ <%= link_to item.title, item.url, target: "_blank" %> ↗︎
<%= item.content %> - <% if item.summary? %> + <% if item.summary? && (!current_user&.hide_ai_summaries?) %>
AI Summary<%= item.summary %>
diff --git a/app/views/user_manager/edit.html.erb b/app/views/user_manager/edit.html.erb index e204348..1206922 100644 --- a/app/views/user_manager/edit.html.erb +++ b/app/views/user_manager/edit.html.erb @@ -19,48 +19,48 @@
<% end %> - +
<%= form.label :email %> <%= form.email_field :email %>
- +
<%= form.label :first_name %> <%= form.text_field :first_name %>
- +
<%= form.label :last_name %> <%= form.text_field :last_name %>
- +
<%= form.label :admin %> <%= form.check_box :admin %>
- +
<%= form.label :subscription_status %> <%= form.select :subscription_status, ['active', 'inactive', 'one_time'], include_blank: true %>
- +
<%= form.label :buttondown_status %> <%= form.select :buttondown_status, ['unactivated', 'regular', 'premium', 'gifted', 'trialed'], include_blank: true %>
- +
<%= form.label :support_type %> <%= form.select :support_type, ['one_time', 'ongoing', 'non_financial'], include_blank: true %>
- +
<%= form.submit 'Update User' %>
<% end %> - + <%= link_to 'Show User', user_manager_path(@user) %> | <%= link_to 'Back to Users', user_manager_path %> - \ No newline at end of file + diff --git a/db/migrate/20241209215122_add_ai_preferences_to_users.rb b/db/migrate/20241209215122_add_ai_preferences_to_users.rb new file mode 100644 index 0000000..9921c78 --- /dev/null +++ b/db/migrate/20241209215122_add_ai_preferences_to_users.rb @@ -0,0 +1,5 @@ +class AddAiPreferencesToUsers < ActiveRecord::Migration[7.2] + def change + add_column :users, :hide_ai_summaries, :boolean, default: false + end +end diff --git a/db/schema.rb b/db/schema.rb index 6d79b3f..dbbdc0a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2024_12_08_070031) do +ActiveRecord::Schema[7.2].define(version: 2024_12_09_215122) do create_table "api_keys", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.string "key" t.datetime "created_at", null: false @@ -76,6 +76,7 @@ ActiveRecord::Schema[7.2].define(version: 2024_12_08_070031) do t.text "otp_secret" t.boolean "otp_required_for_login" t.text "otp_backup_codes" + t.boolean "hide_ai_summaries", default: false t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true -- 2.39.5