Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Adding liquid #260

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions app/models/drops/category_drop.rb
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions app/models/drops/challenge_drop.rb
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions app/models/drops/challenge_file_drop.rb
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions app/models/drops/team_drop.rb
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions app/models/drops/user_drop.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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