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

Add filters to group submissions #132

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
4 changes: 0 additions & 4 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
padding: 0;
}

html {
font-size: 14px;
}

body {
background: #6d3606 image-url("background-1.jpg");
background-size: cover;
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def collection
end

def resource_params
params.require(:group).permit(:name, :visibility, :description)
params.require(:group).permit(:name, :visibility, :description, :starts_at, :ends_at)
end

def initialize_resource
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module ApplicationHelper
ALLOWED_TAGS = %w[b br em i p span strong sub sup table tbody td th thead tr img ul ol li].freeze
ALLOWED_TAGS = %w[b br em i u p span strong sub sup table tbody td th thead tr img ul ol li].freeze

ALLOWED_ATTRIBUTES = %w[class src alt].freeze

Expand Down
2 changes: 1 addition & 1 deletion app/models/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Group < ApplicationRecord

has_many :accepted_users, through: :accepted_memberships, source: :user, class_name: 'User'

has_many :submissions, through: :accepted_users
has_many :submissions, -> (group) { for_group group }, through: :accepted_users

enum visibility: { private: 0, moderated: 1, public: 2 }, _prefix: true
end
12 changes: 12 additions & 0 deletions app/models/submission.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,16 @@ def source_must_be_attached
def update_standings
StandingRedisStore.update_if_exists user_id, problem_id
end

class << self
def for_group group
relation = where problem: group.problems.reorder(nil)

relation = relation.where 'created_at >= ?', group.starts_at if group.starts_at

relation = relation.where 'created_at <= ?', group.ends_at if group.ends_at

relation
end
end
end
6 changes: 5 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class User < ApplicationRecord
GROUP_OPEN_CONDITION = <<-SQL.squish.freeze
(groups.starts_at IS NULL OR groups.starts_at <= NOW()) AND (groups.ends_at IS NULL OR groups.ends_at >= NOW())
SQL

validates :email, presence: true, email: true, uniqueness: { case_sensitive: false }

validates :username, presence: true, uniqueness: { case_sensitive: false }
Expand All @@ -25,7 +29,7 @@ class User < ApplicationRecord

has_many :sharings, through: :accepted_groups

has_many :shared_problems, class_name: 'Problem', through: :sharings, source: :problem
has_many :shared_problems, -> { where GROUP_OPEN_CONDITION }, class_name: 'Problem', through: :sharings, source: :problem

has_secure_password

Expand Down
4 changes: 4 additions & 0 deletions app/views/groups/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@

<%= f.input :description %>

<%= f.input :starts_at, as: :string %>

<%= f.input :ends_at, as: :string %>

<%= f.submit class: 'btn btn-primary' %>
<% end %>
6 changes: 6 additions & 0 deletions db/migrate/20211027221150_add_starts_at_ends_at_to_groups.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddStartsAtEndsAtToGroups < ActiveRecord::Migration[5.2]
def change
add_column :groups, :starts_at, :datetime
add_column :groups, :ends_at, :datetime
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddIndexGroupsOnStartsAtEndsAt < ActiveRecord::Migration[5.2]
def change
add_index :groups, :starts_at
add_index :groups, :ends_at
end
end
6 changes: 5 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2021_04_24_180252) do
ActiveRecord::Schema.define(version: 2021_10_27_225406) do

# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
Expand Down Expand Up @@ -82,8 +82,12 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "description"
t.datetime "starts_at"
t.datetime "ends_at"
t.index ["ends_at"], name: "index_groups_on_ends_at"
t.index ["name"], name: "index_groups_on_name"
t.index ["owner_id"], name: "index_groups_on_owner_id"
t.index ["starts_at"], name: "index_groups_on_starts_at"
end

create_table "logs", force: :cascade do |t|
Expand Down
2 changes: 1 addition & 1 deletion spec/helpers/application_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
end

describe '#sanitize_for_problem' do
let(:tags) { %w[b br em i p span strong sub sup table tbody td th thead tr img ul ol li] }
let(:tags) { %w[b br em i u p span strong sub sup table tbody td th thead tr img ul ol li] }

let(:attributes) { %w[class src alt] }

Expand Down
4 changes: 2 additions & 2 deletions vendor/assets/javascripts/ckeditor.js

Large diffs are not rendered by default.