From 6a6e6f1b30845ae5141b8be58fc2bba6e63d4b77 Mon Sep 17 00:00:00 2001 From: Abdulwahaab Ahmed Date: Sun, 25 Aug 2019 17:07:37 -0400 Subject: [PATCH 1/6] Added liquid gem and basic setup --- Gemfile | 3 +++ Gemfile.lock | 5 +++++ app/controllers/application_controller.rb | 5 +++++ config/application.rb | 1 + 4 files changed, 14 insertions(+) diff --git a/Gemfile b/Gemfile index eaf316f0..beff842a 100644 --- a/Gemfile +++ b/Gemfile @@ -45,6 +45,9 @@ gem 'redcarpet' gem 'sidekiq' +gem 'liquid', '~> 4.0' +gem 'liquid-c', '~> 4.0' + # Use Capistrano for deployment # gem 'capistrano-rails', group: :development diff --git a/Gemfile.lock b/Gemfile.lock index 640e399c..8ecd0754 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -126,6 +126,9 @@ GEM railties (>= 4.2.0) thor (>= 0.14, < 2.0) json (2.2.0) + liquid (4.0.3) + liquid-c (4.0.0) + liquid (>= 3.0.0) listen (3.0.8) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) @@ -311,6 +314,8 @@ DEPENDENCIES guard-rspec jbuilder (~> 2.5) jquery-rails + liquid (~> 4.0) + liquid-c (~> 4.0) listen (~> 3.0.5) pg puma (~> 3.12) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 51ef825d..45628298 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -9,8 +9,13 @@ class ApplicationController < ActionController::Base def user_logged_in? return if logged_in? + store_location flash[:danger] = 'Please log in.' redirect_to login_url end + + def render_liquid_template(template, data: {}) + render html: Liquid::Template.parse(template).render!(data, strict_variables: true).html_safe + end end diff --git a/config/application.rb b/config/application.rb index 0bc0c419..b94f11c3 100644 --- a/config/application.rb +++ b/config/application.rb @@ -24,5 +24,6 @@ class Application < Rails::Application config.logger = ActiveSupport::TaggedLogging.new(logger) config.active_job.queue_adapter = :sidekiq config.autoload_paths << Rails.root.join('lib') + config.autoload_paths << Rails.root.join('app', 'models', 'drops') end end From 30967ae654a64cbc5e8f5f71e3349417f01718ca Mon Sep 17 00:00:00 2001 From: Abdulwahaab Ahmed Date: Sun, 25 Aug 2019 17:08:14 -0400 Subject: [PATCH 2/6] Added category liquid drop --- app/models/drops/category_drop.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 app/models/drops/category_drop.rb diff --git a/app/models/drops/category_drop.rb b/app/models/drops/category_drop.rb new file mode 100644 index 00000000..1e798344 --- /dev/null +++ b/app/models/drops/category_drop.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class CategoryDrop < Liquid::Drop + def initialize(category) + @category = category + end + + def name + @category.name + end + + def challenges + @challenges ||= @category&.challenges&.map { |challenge| ChallengeDrop.new(challenge) } + end +end From f499fd8d363c869b25262616d0b3466bdb324e7c Mon Sep 17 00:00:00 2001 From: Abdulwahaab Ahmed Date: Sun, 25 Aug 2019 17:08:29 -0400 Subject: [PATCH 3/6] Added challenge liquid drop --- app/models/drops/challenge_drop.rb | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 app/models/drops/challenge_drop.rb diff --git a/app/models/drops/challenge_drop.rb b/app/models/drops/challenge_drop.rb new file mode 100644 index 00000000..6fa60f60 --- /dev/null +++ b/app/models/drops/challenge_drop.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +class ChallengeDrop < Liquid::Drop + # TODO: add challenge_files + def initialize(challenge) + @challenge = challenge + end + + def title + @challenge.title + end + + def points + @challenge.points + end + + def max_tries + @challenge.max_tries + end + + def description + @challenge.description.to_md + end + + def raw_description + @challenge.description + end + + def category + @category ||= CategoryDrop.new(@challenge&.category) + end + + def challenge_files + @challenge_files ||= @challenge&.challenge_files&.map { |f| ChallengeFileDrop.new(f) } + end +end From 69831511f6dccd07d0d76aee0873199ade0ee03c Mon Sep 17 00:00:00 2001 From: Abdulwahaab Ahmed Date: Sun, 25 Aug 2019 17:08:42 -0400 Subject: [PATCH 4/6] Added challenge file liquid drop --- app/models/drops/challenge_file_drop.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 app/models/drops/challenge_file_drop.rb diff --git a/app/models/drops/challenge_file_drop.rb b/app/models/drops/challenge_file_drop.rb new file mode 100644 index 00000000..e4003e9e --- /dev/null +++ b/app/models/drops/challenge_file_drop.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class ChallengeFileDrop < Liquid::Drop + def initialize(challenge_file) + @challenge_file = challenge_file + end + + def filename + @challenge_file&.filename&.to_s + end + + def path + Rails.application.routes.url_helpers.rails_blob_path( + @challenge_file, disposition: 'attachment', only_path: true + ) + end +end From 6ec23d6d4546c064772ccb23eebc85532871ac98 Mon Sep 17 00:00:00 2001 From: Abdulwahaab Ahmed Date: Sun, 25 Aug 2019 17:08:58 -0400 Subject: [PATCH 5/6] Added team liquid drop --- app/models/drops/team_drop.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 app/models/drops/team_drop.rb diff --git a/app/models/drops/team_drop.rb b/app/models/drops/team_drop.rb new file mode 100644 index 00000000..02653739 --- /dev/null +++ b/app/models/drops/team_drop.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +class TeamDrop < Liquid::Drop + def initialize(team) + @team = team + end + + def name + @team.name + end + + def invitation_token + @team&.invitation_token + end + + def score + @team&.score + end + + def users + @users ||= @team&.users&.map { |user| UserDrop.new(user) } + end +end From af33afb577869e9d55b3642d43e9c76f6e1778c1 Mon Sep 17 00:00:00 2001 From: Abdulwahaab Ahmed Date: Sun, 25 Aug 2019 17:09:18 -0400 Subject: [PATCH 6/6] Added team user drop --- app/models/drops/user_drop.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 app/models/drops/user_drop.rb diff --git a/app/models/drops/user_drop.rb b/app/models/drops/user_drop.rb new file mode 100644 index 00000000..e388ee37 --- /dev/null +++ b/app/models/drops/user_drop.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class UserDrop < Liquid::Drop + def initialize(user) + @user = user + end + + def name + @user.name + end + + def username + @user.username + end + + def team + @team ||= TeamDrop.new(@user&.team) + end +end