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: Event & Org support #17

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
3400b50
Add recommendations for Events and Organizations
gdpelican Feb 13, 2019
bcd6769
Merge branch 'gdpelican/misc' into gdpelican/event-organisations
gdpelican Mar 16, 2019
b873195
Fix error on me page
gdpelican Mar 16, 2019
4ae46bf
Merge remote-tracking branch 'james/gdpelican/event-organisations' in…
Mar 25, 2019
381f1d2
Use datepicker fields for event datetime inputs.
Mar 25, 2019
6fdeba4
Event list mockup.
Mar 26, 2019
58856d5
Merge remote-tracking branch 'origin/master' into feature/events-orga…
Mar 26, 2019
86fd75a
Next camps under events and fix some initial routes.
Mar 26, 2019
89df6b4
Update camp routes to include event.
Mar 28, 2019
862c820
Merge remote-tracking branch 'origin/master' into feature/events-orga…
Mar 28, 2019
fc329f6
Better routing for events.
Mar 28, 2019
1a955a0
Merge remote-tracking branch 'origin/master' into feature/events-orga…
Mar 28, 2019
620e8a1
Merge remote-tracking branch 'origin/master' into feature/events-orga…
Mar 28, 2019
573b9ef
Re-remove haml form.
Mar 28, 2019
b38e7c2
Merge remote-tracking branch 'origin/master' into feature/events-orga…
Mar 28, 2019
e6c6891
Fixed a route issue & redirects for bad urls
Mar 28, 2019
bf633e3
Per-event granting wip.
Mar 28, 2019
3b9d3df
Merge remote-tracking branch 'origin/master' into feature/events-orga…
Mar 28, 2019
bbda179
Update routes again to make url helpers nicer.
Mar 29, 2019
24cb1eb
Granting workflow working.
Mar 29, 2019
f8c97fb
Merge remote-tracking branch 'origin/master' into feature/events-orga…
Mar 29, 2019
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
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -496,4 +496,4 @@ RUBY VERSION
ruby 2.5.1p57

BUNDLED WITH
1.15.2
2.0.1
3 changes: 3 additions & 0 deletions app/admin/dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

menu priority: 1, label: proc{ I18n.t("active_admin.dashboard") }

# TODO: Adding some information about events and organizations would likely
# be a welcome addition to this dashboard

content title: proc{ I18n.t("active_admin.dashboard") } do
default_coins = 10

Expand Down
18 changes: 18 additions & 0 deletions app/admin/event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
ActiveAdmin.register Event do
actions :index, :show, :new, :create, :edit, :update, :destroy
permit_params :organization_id, :name, :slug, :submission_deadline, :safety_deadline, :starts_at, :ends_at

form do |f|
f.semantic_errors
f.inputs do
f.input :name
f.input :slug
f.input :organization, as: :select, collection: Organization.all
f.input :submission_deadline, as: :datepicker
f.input :safety_deadline, as: :datepicker
f.input :starts_at, as: :datepicker
f.input :ends_at, as: :datepicker
end
f.actions
end
end
41 changes: 41 additions & 0 deletions app/admin/membership.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
ActiveAdmin.register Membership do
actions :index, :show, :new, :create, :edit, :update
permit_params :user_id, :collective_id, :collective_type

form do |f|
f.inputs
panel "hejsan" do
end
end

# TODO: currently this doesn't quite work to create memberships the way we'd like
# A first pass solution might be to have a dropdown of users by email
# and a dropdown of all Camps and Organizations combined, so that you can choose
# who to put in what collective.

# SUPER ROUGH mockup might be:
# New Membership
# --------------

# User
# ----
# Hugi ([email protected])
# Kristoffer ([email protected])

# Collective
# ----------
# West World (organization)
# East World (organization)
# Sea Snugs (camp)
# Health + Safety wizards (camp)

# You should be able to find a way to modify the display_name of a model in the ActiveAdmin docs

# ALTERNATIVELY: you could create two dashboards here,
# (maybe admin/camp_memberships.rb and admin/organization_memberships.rb)
# one for adding people to organizations, and another for adding them to camps.

# A THIRD OPTION: Skip being able to add Camp memberships here
# (that works in the app anyway), and simply make this form
# able to add memberships to organizations
end
4 changes: 4 additions & 0 deletions app/admin/organization.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ActiveAdmin.register Organization do
actions :index, :show, :new, :create, :edit, :update, :destroy
permit_params :name
end
6 changes: 3 additions & 3 deletions app/assets/javascripts/camps.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ $(document).ready(function() {

$.ajax({
url: url,
type: 'POST',
data: {method:'_patch', 'camp[tag]': tag, authenticity_token:token},
type: 'DELETE',
data: {_method:'DELETE', 'camp[tag]': tag, authenticity_token:token},
success: function(data) {
refreshTags(data);
}
Expand All @@ -76,7 +76,7 @@ $(document).ready(function() {
$.ajax({
url: url,
type: 'POST',
data: {method:'_patch', 'camp[tag_list]': tags, authenticity_token:token},
data: {'camp[tag_list]': tags, authenticity_token:token},
success: function(data) {
refreshTags(data);
}
Expand Down
54 changes: 30 additions & 24 deletions app/controllers/camps_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class CampsController < ApplicationController

before_action :apply_filters, only: :index
before_action :authenticate_user!, except: [:show, :index]
before_action :load_event!
before_action :load_camp!, except: [:index, :new, :create]
before_action :ensure_admin_delete!, only: [:destroy, :archive]
before_action :ensure_admin_update!, only: [:update]
Expand All @@ -15,6 +16,7 @@ def index

def new
@camp = Camp.new

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@camp = @event.camps.build

@camp.event = @event
end

def edit
Expand All @@ -30,7 +32,7 @@ def create
@camp)

flash[:notice] = t('created_new_dream')
redirect_to edit_camp_path(id: @camp.id)
redirect_to edit_event_camp_path(@event, @camp)
else
flash.now[:notice] = "#{t:errors_str}: #{@camp.errors.full_messages.uniq.join(', ')}"
render :new
Expand All @@ -41,36 +43,30 @@ def create

def toggle_granting
@camp.toggle!(:grantingtoggle)
redirect_to camp_path(@camp)
redirect_to event_camp_path(@event, @camp)
end

def update_grants
@camp.grants.build(user: current_user, amount: granted)
@camp.assign_attributes(
minfunded: (@camp.grants_received + granted) >= @camp.minbudget,
fullyfunded: (@camp.grants_received + granted) >= @camp.maxbudget
)

if @camp.save
current_user.update(grants: current_user.grants - granted)
flash[:notice] = t(:thanks_for_sending, grants: granted)
actually_granted, ok = current_user.wallet_for(@event).grant_to(@camp, granted)
if ok
flash[:notice] = t(:thanks_for_sending, grants: actually_granted)
else
flash[:error] = t(:errors_str, message: @camp.errors.full_messages.uniq.join(', '))
end

redirect_to camp_path(@camp)
redirect_to event_camp_path(@event, @camp)
end

def update
if @camp.update_attributes camp_params
if params[:done] == '1'
redirect_to camp_path(@camp)
redirect_to event_camp_path(@event, @camp)
elsif params[:safetysave] == '1'
puts(camp_safety_sketches_path(@camp))
redirect_to camp_safety_sketches_path(@camp)
puts(event_camp_safety_sketches_path(@event, @camp))
redirect_to event_camp_safety_sketches_path(@event, @camp)
else
respond_to do |format|
format.html { redirect_to edit_camp_path(@camp) }
format.html { redirect_to edit_event_camp_path(@event, @camp) }
format.json { respond_with_bip(@camp) }
end
end
Expand Down Expand Up @@ -103,7 +99,7 @@ def remove_tag_params

def destroy
@camp.destroy!
redirect_to camps_path
redirect_to event_camps_path(@event)
end

# Display a camp and its users
Expand Down Expand Up @@ -138,11 +134,16 @@ def toggle_favorite

def archive
@camp.update!(active: false)
redirect_to camps_path
redirect_to event_camps_path(@event)
end

private

def load_event!
@event = Event.find(params[:event_id])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not_found unless @event = Event.find(params[:event_id])

not_found if @event.nil?
end

# TODO: We can't permit! attributes like this, because it means that anyone
# can update anything about a camp in any way (including the id, etc); recipe for disaster!
# we'll have to go through and determine which attributes can actually be updated via
Expand All @@ -152,9 +153,12 @@ def camp_params
end

def load_camp!
return if @camp = Camp.find_by(params.permit(:id))
flash[:alert] = t(:dream_not_found)
redirect_to camps_path
@camp = Camp.includes(:event).find(params[:id])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this wants to be

def load_camp!
  return unless @camp = @event.camps.find_by(params[:id])
  flash[:alert] = t(:dream_not_found)
  redirect_to event_camps_path(@event)
end


if @camp.nil? or @camp.event.id != @event.id
flash[:alert] = t(:dream_not_found)
redirect_to event_camps_path(@event)
end
end

def ensure_admin_delete!
Expand All @@ -170,11 +174,13 @@ def ensure_admin_update!
end

def ensure_grants!
grants_left = current_user.grants_left_for(@event)

assert(@camp.maxbudget, :dream_need_to_have_max_budget) ||
assert(current_user.grants >= granted, :security_more_grants, granted: granted, current_user_grants: current_user.grants) ||
assert(grants_left >= granted, :security_more_grants, granted: granted, current_user_grants: grants_left) ||
assert(granted > 0, :cant_send_less_then_one) ||
assert(
current_user.admin || (@camp.grants.where(user: current_user).sum(:amount) + granted <= app_setting('max_grants_per_user_per_dream')),
current_user.admin || (@camp.grants_for(@event).where(user: current_user).sum(:amount) + granted <= app_setting('max_grants_per_user_per_dream')),
:exceeds_max_grants_per_user_for_this_dream,
max_grants_per_user_per_dream: app_setting('max_grants_per_user_per_dream')
)
Expand All @@ -185,7 +191,7 @@ def granted
end

def failure_path
camp_path(@camp)
event_camp_path(@event, @camp)
end

def create_camp
Expand Down
37 changes: 37 additions & 0 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class EventsController < ApplicationController
def index
@events = Event.all.order(starts_at: :desc)
end

def show
# TODO: We should provide an option on the events#show page to view
# all past or future events, which will allow users to navigate to
# events which are not the current one
@event = Event.find(params[:id])
redirect_to events_path if @event.nil?
end

def current
@event = Event.current
render :show
end

def future
@events = Event.future
render :index
end

def past
@events = Event.past
render :index
end

def redirect_to_most_relevant
e = Event.most_relevant
unless e.nil?
redirect_to event_camps_path(e)
else
redirect_to events_path
end
end
end
13 changes: 9 additions & 4 deletions app/controllers/images_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,26 @@ def index

def create
if Image.create(image_params)
redirect_to camp_images_path(params.permit(:camp_id))
redirect_to event_camp_images_path(@event, @camp)
else
render action: :index
end
end

def destroy
@camp.images.find(params[:id]).destroy!
redirect_to camp_images_path(params.permit(:camp_id))
redirect_to event_camp_images_path(@event, @camp)
end

private

def load_camp!
@camp = Camp.find(params[:camp_id])
@event = Event.find(params[:event_id])
not_found if @event.nil? if @event.nil?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might consider pulling out load_camp! and load_event! into a module so you don't have to define them in multiple places; this loading code is important, reusable, and shouldn't be defined in more than one place.

At the very least you should drop the double if @event.nil? here :)


@camp = Camp.includes(:event).find(params[:camp_id])
redirect_to event_camps_path(@event) if @camp.nil? or @camp.event.id != @event.id

assert(current_user == @camp.creator || current_user.admin, :security_cant_change_images_you_dont_own)
end

Expand All @@ -31,7 +36,7 @@ def ensure_image!
end

def failure_path
camp_images_path(params.permit(:camp_id))
event_camp_images_path(@event, @camp)
end

def image_params
Expand Down
13 changes: 13 additions & 0 deletions app/controllers/organizations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class OrganizationsController < ApplicationController
def show
# TODO: More than likely you'll want to create a view of an organization,
# which displays the name of the org and perhaps lists the events they've done
# That goes here!

# This will likely eventually look like an 'About the organization' page, which
# is visible to the public and provides additional info about the org to anyone
# who signs up

@organization = Organization.find(params[:id])
end
end
22 changes: 6 additions & 16 deletions app/controllers/users/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,17 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController

def facebook
@user = User.from_omniauth(request.env["omniauth.auth"])

if @user.persisted?
sign_in_and_redirect @user, :event => :authentication
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end

sign_in_and_redirect @user, :event => :authentication
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
end

def saml
@user = User.from_omniauth(request.env["omniauth.auth"])

if @user.persisted?
c = Rails.application.config.x.firestarter_settings
sign_in_and_redirect @user, event: :authentication #this will throw if @user is not activated
set_flash_message(:notice, :success, kind: c['saml_human_name']) if is_navigational_format?
else
session["devise.saml_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
c = Rails.application.config.x.firestarter_settings

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a helper for this now; should be app_settings('saml_human_name').

set_flash_message :notice, :success, kind: app_settings('saml_human_name')

You can include the AppSettings concern in this controller if that doesn't work straight out of the box.

sign_in_and_redirect @user, event: :authentication #this will throw if @user is not activated
set_flash_message(:notice, :success, kind: c['saml_human_name']) if is_navigational_format?
end

def failure
Expand Down
8 changes: 6 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ class UsersController < ApplicationController
before_action :load_lang_detector, only: [:show, :index, :me]

def me
@memberships = current_user.created_camps.joins(:memberships).joins(:users)
.where('users.id != ?', current_user.id).select('users.email')
# TODO: There's a possible regression here where this list may now display
# Organization memberships as well.
# I've written a relation which may be able to replace it cleanly in user.rb#18,
# so you could start off with seeing if something like the following works:

@memberships = current_user.collaborators.distinct.pluck(:email) - Array(current_user.email)
end

def show
Expand Down
5 changes: 4 additions & 1 deletion app/models/camp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ class Camp < ApplicationRecord
include AppSettings
extend AppSettings
belongs_to :creator, class_name: 'User', foreign_key: 'user_id'
belongs_to :event

has_many :memberships, dependent: :destroy
has_many :memberships, as: :collective, dependent: :destroy
has_many :users, through: :memberships
has_many :favorites
has_many :favorite_users, through: :favorites, source: :user
has_many :images #, :dependent => :destroy
has_many :safety_sketches

has_many :grants

has_many :budget_items
has_many :safety_items

Expand Down
Loading