diff --git a/.env.example b/.env.example index e8f8e79a..4249163f 100644 --- a/.env.example +++ b/.env.example @@ -4,4 +4,5 @@ CONVERTKIT_FORM_ID= CONVERTKIT_TAG_ID= ADMIN_USERNAME= ADMIN_PASSWORD= -SYNTH_API_KEY= \ No newline at end of file +SYNTH_API_KEY= +BANNERBEAR_API_KEY= \ No newline at end of file diff --git a/Gemfile b/Gemfile index 5f9c99f9..c3e43f03 100644 --- a/Gemfile +++ b/Gemfile @@ -28,6 +28,7 @@ gem "tzinfo-data", platforms: %i[ windows jruby ] gem "redcarpet" gem "avo", ">= 3.2" gem "revise_auth" +gem "bannerbear" group :development, :test do diff --git a/Gemfile.lock b/Gemfile.lock index dce43493..970c43a1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -127,6 +127,8 @@ GEM turbo_power (>= 0.6.0) view_component (>= 3.7.0) zeitwerk (>= 2.6.12) + bannerbear (0.1.4) + httparty base64 (0.2.0) bcrypt (3.1.20) better_html (2.1.1) @@ -413,6 +415,7 @@ PLATFORMS DEPENDENCIES avo (>= 3.2) + bannerbear bootsnap brakeman capybara diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index b451bf4c..c7f4bd9d 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -7,6 +7,10 @@ def description(page_description) content_for(:description) { page_description } end + def meta_image(meta_image) + content_for(:meta_image) { meta_image } + end + def markdown(text) options = { filter_html: true, diff --git a/app/models/article.rb b/app/models/article.rb index 9e8046a2..fb0a0128 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -1,4 +1,6 @@ class Article < ApplicationRecord + include MetaImage + def self.random_sample(count, exclude:) where.not(id: exclude.id).order(Arel.sql("RANDOM()")).limit(count) end @@ -6,4 +8,10 @@ def self.random_sample(count, exclude:) def to_param slug end + + private + + def create_meta_image + super(title) + end end diff --git a/app/models/concerns/meta_image.rb b/app/models/concerns/meta_image.rb new file mode 100644 index 00000000..fa599cdb --- /dev/null +++ b/app/models/concerns/meta_image.rb @@ -0,0 +1,28 @@ +module MetaImage + extend ActiveSupport::Concern + + included do + after_commit :create_meta_image, on: [ :create, :update ] + end + + private + + def create_meta_image(custom_text) + bb = Bannerbear::Client.new + begin + response = bb.create_image("RnxGpW5lvKw0bEXrJ1", + synchronous: true, + modifications: [ + { + name: "text", + text: custom_text + } + ] + ) + + self.update_columns(meta_image_url: response["image_url"]) + rescue => e + Rails.logger.error "Failed to create image: #{e.message}" + end + end +end diff --git a/app/models/term.rb b/app/models/term.rb index 96ef5d18..a22cceda 100644 --- a/app/models/term.rb +++ b/app/models/term.rb @@ -1,4 +1,6 @@ class Term < ApplicationRecord + include MetaImage + def self.random_sample(count, exclude:) where.not(id: exclude.id).order(Arel.sql("RANDOM()")).limit(count) end @@ -6,4 +8,10 @@ def self.random_sample(count, exclude:) def to_param slug end + + private + + def create_meta_image + super(title) + end end diff --git a/app/models/tool.rb b/app/models/tool.rb index 89ad8fc9..1403ad67 100644 --- a/app/models/tool.rb +++ b/app/models/tool.rb @@ -1,4 +1,6 @@ class Tool < ApplicationRecord + include MetaImage + CATEGORIES = { retirement: { name: "Retirement", @@ -39,4 +41,10 @@ def to_param def category CATEGORIES[category_slug&.to_sym] end + + private + + def create_meta_image + super(name) + end end diff --git a/app/views/articles/show.html.erb b/app/views/articles/show.html.erb index d03646c6..9432b014 100644 --- a/app/views/articles/show.html.erb +++ b/app/views/articles/show.html.erb @@ -1,6 +1,7 @@ <% title @article.title description "#{markdown(@article.content).gsub(%r{?[^>]+?>}, '')[0...300]}..." + meta_image @article.meta_image_url if @article.meta_image_url.present? %> <%= link_to articles_path, class: "w-9 h-9 rounded-full grid place-items-center mx-auto text-gray-500 hover:bg-gray-200/50 bg-transparent border border-gray-300 hover:border-gray-300 " do %> diff --git a/app/views/terms/show.html.erb b/app/views/terms/show.html.erb index 43b6200e..49239220 100644 --- a/app/views/terms/show.html.erb +++ b/app/views/terms/show.html.erb @@ -1,6 +1,7 @@ <% title "#{@term.title} - Financial Terms" description "#{markdown(@term.content).gsub(%r{?[^>]+?>}, '')[0...300]}..." + meta_image @term.meta_image_url if @term.meta_image_url.present? %> <%= link_to terms_path, class: "w-9 h-9 rounded-full grid place-items-center mx-auto text-gray-500 hover:bg-gray-200/50 bg-transparent border border-gray-300 hover:border-gray-300 " do %> diff --git a/app/views/tools/_compound_interest_calculator.html.erb b/app/views/tools/_compound_interest_calculator.html.erb index d39c7d9a..c1f1eb3a 100644 --- a/app/views/tools/_compound_interest_calculator.html.erb +++ b/app/views/tools/_compound_interest_calculator.html.erb @@ -1,8 +1,3 @@ -<% - title "Compound Interest Calculator" - description "See how your investments grow over time by earning interest on interest and letting your money work for you." -%> -
Fill in the fields to calculate your investment's ROI.
-Fill in the fields to calculate your investment's ROI.
Something that costs you today, will cost you
+ +after years at % inflation annually
Something that costs you today, will cost you
- -after years at % inflation annually
+That's an increase of
+That's an increase of
- -Which comes to
- -Which comes to
+if you do nothing with that it'll be worth
+ +after years at % inflation annually
+if you do nothing with that it'll be worth
- -after years at % inflation annually
+That's an decrease of
+That's an decrease of
- -Which comes to
- -Which comes to
+On average, considering fluctuations, the global inflation rate has hovered around 3% to 3.5% per year over the last decade.
On average, considering fluctuations, the global inflation rate has hovered around 3% to 3.5% per year over the last decade.
+Fill in the fields to calculate inflation.
-Fill in the fields to calculate inflation.
+
<%= link_to "Tools", tools_path %> / <%= @tool.name %>