Skip to content

Commit

Permalink
Triplicate conferences hacking the hashtag (#7)
Browse files Browse the repository at this point in the history
* add custom menu items

* controller override, add routes

* change add item menu

* Apply suggestions from code review

Co-authored-by: Ivan Vergés <ivan@pokecode.net>

* add options if to add_item

* add rspec, refactoring

* fix lint

* promoted conferences override, add tests

* remove global config from secret, refactoring controller's methods

* change tests

* add controller spec

* fix robocop

* move initializers and overrides to initializers folder

* refactoring model and tests

* Update config/locales/en.yml

Co-authored-by: Ivan Vergés <ivan@pokecode.net>

* fix initializers

* fix initialize

* gitignore

* add actions

* add config

---------

Co-authored-by: Ivan Vergés <ivan@pokecode.net>
  • Loading branch information
antopalidi and microstudi committed Aug 21, 2023
1 parent de4ba74 commit 21226b7
Show file tree
Hide file tree
Showing 23 changed files with 714 additions and 69 deletions.
64 changes: 64 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Test

on:
push:
branches:
- main
- staging
pull_request:

env:
RUBY_VERSION: 3.0.6
NODE_VERSION: 16.9.1

jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:11
ports:
- 5432:5432
env:
RAILS_ENV: test
DATABASE_USERNAME: postgres
DATABASE_PASSWORD: postgres
POSTGRES_HOST_AUTH_METHOD: trust
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

steps:
- uses: actions/checkout@v2
with:
fetch-depth: 1

- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ env.RUBY_VERSION }}
bundler-cache: true

- run: bundle exec rubocop -P
name: Lint Ruby files

- name: Setup & create Database
run: |
bundle exec rails db:create db:schema:load
env:
RAILS_ENV: test
DATABASE_USERNAME: postgres
DATABASE_PASSWORD: postgres

- name: Precompile assets
run: |
npm ci
bundle exec rake assets:precompile
env:
RAILS_ENV: production
DB_ADAPTER: nulldb
SECRET_KEY_BASE: 1234567890

- name: Run RSpec
run: SIMPLECOV=1 CODECOV=1 bundle exec rspec
env:
RAILS_ENV: test
DATABASE_USERNAME: postgres
DATABASE_PASSWORD: postgres
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ yarn-debug.log*
.rbenv-vars
public/decidim-packs
public/sw.js*
coverage/*
.rspec-*
5 changes: 0 additions & 5 deletions .rubocop_ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ AllCops:
# Otherwise we fallback to the oldest officially supported Ruby version (2.0).
TargetRubyVersion: 2.7

RSpec:
Patterns:
- "(?:^|/)spec/"
- "(?:^|/)test/"

# Indent private/protected/public as deep as method definitions
Layout/AccessModifierIndentation:
EnforcedStyle: indent
Expand Down
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ gem "wicked_pdf", "~> 2.1"
gem "deface", ">= 1.9"

group :development, :test do
gem "faker", "~> 2.14"
gem "byebug", "~> 11.0", platform: :mri
gem "faker", "~> 2.14"

gem "brakeman"
gem "decidim-dev", DECIDIM_VERSION
Expand All @@ -36,9 +36,9 @@ end
group :development do
gem "letter_opener_web"
gem "listen"
gem "rubocop-faker"
gem "spring"
gem "spring-watcher-listen"
gem "rubocop-faker"
gem "web-console"
end

Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,7 @@ GEM

PLATFORMS
x86_64-darwin-22
x86_64-darwin-23
x86_64-linux

DEPENDENCIES
Expand Down
60 changes: 60 additions & 0 deletions app/controllers/concerns/conferences_controller_override.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# frozen_string_literal: true

module ConferencesControllerOverride
extend ActiveSupport::Concern

included do
def hashtag_for_current_url
@hashtag_for_current_url ||= Rails.application.secrets.custom_conference_types.find do |item|
item[:url] == request.path
end&.dig(:hashtag)
end

def published_conferences
@published_conferences ||= conferences_query(Decidim::Conferences::OrganizationPublishedConferences)
end

def conferences
@conferences ||= conferences_query(Decidim::Conferences::OrganizationPrioritizedConferences)
end

alias_method :collection, :conferences

def promoted_conferences
@promoted_conferences ||= hashtag_for_current_url ? promoted_filtered_by_hashtag(conferences) : promoted_conferences_without_custom_hashtag(Decidim::Conference.promoted)
end

private

def custom_hashtags
@custom_hashtags ||= Rails.application.secrets.custom_conference_types.map { |item| item[:hashtag] }
end

def conferences_query(conference_class)
conferences = conference_class.new(current_organization, current_user)
hashtag_for_current_url ? conferences_with_custom_hashtag_filter(conferences) : conferences_without_custom_hashtag(conferences)
end

def conferences_with_custom_hashtag_filter(conferences)
conferences.query.merge(Decidim::Conference.filtered_by_hashtag(hashtag_for_current_url))
end

def conferences_without_custom_hashtag(conferences)
conferences.query.to_a.reject do |conference|
conference_hashtags = conference.hashtag.split
(conference_hashtags & custom_hashtags).any?
end
end

def promoted_filtered_by_hashtag(conferences)
conferences.merge(Decidim::Conference.promoted.filtered_by_hashtag(hashtag_for_current_url))
end

def promoted_conferences_without_custom_hashtag(conferences)
conferences.select do |conference|
conference_hashtags = conference.hashtag.split
(conference_hashtags & custom_hashtags).empty?
end
end
end
end
11 changes: 11 additions & 0 deletions app/models/concerns/conference_override.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module ConferenceOverride
extend ActiveSupport::Concern

included do
def self.filtered_by_hashtag(hashtag)
where("(CONCAT(' ', hashtag, ' ') ILIKE ?) OR (CONCAT(' ', hashtag, ' ') ILIKE ?)", "% #{hashtag} %", "% ##{hashtag} %")
end
end
end
2 changes: 1 addition & 1 deletion bin/spring
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

if !defined?(Spring) && [nil, "development", "test"].include?(ENV["RAILS_ENV"])
if !defined?(Spring) && [nil, "development", "test"].include?(ENV.fetch("RAILS_ENV", nil))
gem "bundler"
require "bundler"

Expand Down
2 changes: 1 addition & 1 deletion bin/webpack
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

ENV["RAILS_ENV"] ||= ENV["RACK_ENV"] || "development"
ENV["RAILS_ENV"] ||= ENV.fetch("RACK_ENV", nil) || "development"
ENV["NODE_ENV"] ||= "development"

require "pathname"
Expand Down
2 changes: 1 addition & 1 deletion bin/webpack-dev-server
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

ENV["RAILS_ENV"] ||= ENV["RACK_ENV"] || "development"
ENV["RAILS_ENV"] ||= ENV.fetch("RACK_ENV", nil) || "development"
ENV["NODE_ENV"] ||= "development"

require "pathname"
Expand Down
53 changes: 0 additions & 53 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,58 +22,5 @@ class Application < Rails::Application
#
# config.time_zone = "Central Time (US & Canada)"
# config.eager_load_paths << Rails.root.join("extras")

# generic overrides
config.to_prepare do
Decidim::StatisticCell.include(Decidim::StatisticCellOverride)
end

initializer "decidim.core.homepage_content_blocks" do
Decidim.content_blocks.register(:homepage, :active_processes) do |content_block|
content_block.cell = "decidim/content_blocks/active_processes"
content_block.settings_form_cell = "decidim/content_blocks/active_processes_settings_form"
content_block.public_name_key = "decidim.content_blocks.active_processes.name"

content_block.settings do |settings|
settings.attribute :button_text, type: :text, translated: true
settings.attribute :button_url, type: :text

(1..3).each do |i|
settings.attribute "link_url_#{i}".to_sym, type: :text
settings.attribute "link_text_#{i}".to_sym, type: :text, translated: true
settings.attribute "text_color_#{i}".to_sym, type: :string
end
end

(1..3).each do |i|
content_block.images << {
name: "image_#{i}".to_sym,
uploader: "Decidim::ActiveProcessesImageUploader"
}
end

content_block.default!
end

Decidim.content_blocks.register(:homepage, :extended_hero) do |content_block|
content_block.cell = "decidim/content_blocks/extended_hero"
content_block.settings_form_cell = "decidim/content_blocks/extended_hero_settings_form"
content_block.public_name_key = "decidim.content_blocks.extended_hero.name"

content_block.settings do |settings|
settings.attribute :welcome_title, type: :text, translated: true
settings.attribute :subtitle, type: :text, translated: true
end

content_block.images = [
{
name: :background_image,
uploader: "Decidim::HomepageImageUploader"
}
]

content_block.default!
end
end
end
end
10 changes: 4 additions & 6 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
config.public_file_server.enabled = ENV["RAILS_SERVE_STATIC_FILES"].present?

# Enable serving of images, stylesheets, and JavaScripts from an asset server.
config.asset_host = ENV["RAILS_ASSET_HOST"] if ENV["RAILS_ASSET_HOST"].present?
config.asset_host = ENV.fetch("RAILS_ASSET_HOST", nil) if ENV["RAILS_ASSET_HOST"].present?

# Specifies the header that your server uses for sending files.
# config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache
Expand All @@ -46,7 +46,7 @@

# Include generic and useful information about system operation, but avoid logging too much
# information to avoid inadvertent exposure of personally identifiable information (PII).
config.log_level = %w(debug info warn error fatal).include?(ENV["RAILS_LOG_LEVEL"]) ? ENV["RAILS_LOG_LEVEL"] : :info
config.log_level = %w(debug info warn error fatal).include?(ENV.fetch("RAILS_LOG_LEVEL", nil)) ? ENV["RAILS_LOG_LEVEL"] : :info

# Prepend all log lines with the following tags.
config.log_tags = [:request_id]
Expand All @@ -55,7 +55,7 @@
# config.cache_store = :mem_cache_store

# Use a real queuing backend for Active Job (and separate queues per environment).
config.active_job.queue_adapter = ENV["QUEUE_ADAPTER"] if ENV["QUEUE_ADAPTER"].present?
config.active_job.queue_adapter = ENV.fetch("QUEUE_ADAPTER", nil) if ENV["QUEUE_ADAPTER"].present?
# config.active_job.queue_name_prefix = "decidim_staging_production"

config.action_mailer.perform_caching = false
Expand Down Expand Up @@ -124,7 +124,5 @@
# config.active_record.database_resolver = ActiveRecord::Middleware::DatabaseSelector::Resolver
# config.active_record.database_resolver_context = ActiveRecord::Middleware::DatabaseSelector::Resolver::Session

if config.respond_to?(:deface)
config.deface.enabled = ENV["DB_ADAPTER"].blank? || ENV["DB_ADAPTER"] == "postgresql"
end
config.deface.enabled = ENV["DB_ADAPTER"].blank? || ENV.fetch("DB_ADAPTER", nil) == "postgresql" if config.respond_to?(:deface)
end
76 changes: 76 additions & 0 deletions config/initializers/conferences_hacks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# frozen_string_literal: true

# extra menus defined in secrets.yml
Decidim.menu :menu do |menu|
custom_conference_types = Rails.application.secrets.custom_conference_types || []
custom_conference_types.each do |item|
hashtag = item[:hashtag]
options = {}
options[:position] = item[:position].to_i if item[:position]
options[:active] = item[:active].to_sym if item[:active]
options[:icon_name] = item[:icon_name].to_s if item[:icon_name]
options[:if] = Decidim::Conference.exists?(Decidim::Conference.filtered_by_hashtag(hashtag))
menu.add_item item[:key], I18n.t(item[:key], scope: "decidim.conferences.custom_conference_types"), item[:url], options
end
end

Rails.application.config.to_prepare do
Decidim::StatisticCell.include(Decidim::StatisticCellOverride)
Decidim::Conference.include(ConferenceOverride)
end

Rails.application.config.after_initialize do
Decidim::Conferences::ConferencesController.include(ConferencesControllerOverride)
end

Rails.application.config do
initializer "capitalitat.homepage_content_blocks" do
config.to_prepare do
Decidim.content_blocks.register(:homepage, :active_processes) do |content_block|
content_block.cell = "decidim/content_blocks/active_processes"
content_block.settings_form_cell = "decidim/content_blocks/active_processes_settings_form"
content_block.public_name_key = "decidim.content_blocks.active_processes.name"

content_block.settings do |settings|
settings.attribute :button_text, type: :text, translated: true
settings.attribute :button_url, type: :text

(1..3).each do |i|
settings.attribute "link_url_#{i}".to_sym, type: :text
settings.attribute "link_text_#{i}".to_sym, type: :text, translated: true
settings.attribute "text_color_#{i}".to_sym, type: :string
end
end

(1..3).each do |i|
content_block.images << {
name: "image_#{i}".to_sym,
uploader: "Decidim::ActiveProcessesImageUploader"
}
end

content_block.default!
end

Decidim.content_blocks.register(:homepage, :extended_hero) do |content_block|
content_block.cell = "decidim/content_blocks/extended_hero"
content_block.settings_form_cell = "decidim/content_blocks/extended_hero_settings_form"
content_block.public_name_key = "decidim.content_blocks.extended_hero.name"

content_block.settings do |settings|
settings.attribute :welcome_title, type: :text, translated: true
settings.attribute :subtitle, type: :text, translated: true
end

content_block.images = [
{
name: :background_image,
uploader: "Decidim::HomepageImageUploader"
}
]

content_block.default!
end
end
end
end
4 changes: 4 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
---
en:
decidim:
conferences:
custom_conference_types:
city: City conferences
parallel: Parallel conferences
content_blocks:
active_processes:
name: Active processes
Expand Down

0 comments on commit 21226b7

Please sign in to comment.