forked from openstreetmap/openstreetmap-website
-
Notifications
You must be signed in to change notification settings - Fork 1
/
microcosms_controller.rb
106 lines (89 loc) · 2.95 KB
/
microcosms_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
class MicrocosmsController < ApplicationController
layout "site"
before_action :authorize_web
before_action :set_microcosm, :only => [:edit, :show, :show_events, :show_members, :step_up, :update]
helper_method :recent_changesets
load_and_authorize_resource :except => [:create, :new]
authorize_resource
def index
# TODO: OMG is the math right here?
minute_of_day = "(60 * EXTRACT(HOUR FROM current_timestamp) + EXTRACT(MINUTE FROM current_timestamp))"
morning = "(60 * 6)" # 6 AM
long_facing_sun = "(#{minute_of_day} + #{morning}) / 4"
# Using Arel.sql here due to warning about non-attributes arguments will be disallowed in Rails 6.1.
@microcosms = Microcosm.order(Arel.sql("longitude + 180 + #{long_facing_sun} DESC"))
end
# GET /microcosms/mycity
# GET /microcosms/mycity.json
def show
@my_membership = MicrocosmMember.find_or_initialize_by(:microcosm_id => @microcosm.id, :user_id => current_user&.id)
end
def show_members
# Could use pluralize, but we don't need that at this time.
@roles = MicrocosmMember::Roles::ALL_ROLES.map { |r| r + "s" }
end
def show_events; end
def edit; end
def update
if @microcosm.update(microcosm_params)
redirect_to @microcosm, :notice => t(".success")
else
flash[:alert] = t(".failure")
render :edit
end
end
def new
@title = t "microcosms.new.title"
@microcosm = Microcosm.new
end
def create
@microcosm = Microcosm.new(microcosm_params)
if @microcosm.save && add_first_organizer
redirect_to @microcosm, :notice => t(".success")
else
flash[:alert] = t(".failure")
render "new"
end
end
def add_first_organizer
params = { :microcosm_id => @microcosm.id, :user_id => current_user.id, :role => MicrocosmMember::Roles::ORGANIZER }
membership = MicrocosmMember.new(params)
membership.save
end
def recent_changesets
bbox = BoundingBox.new(@microcosm.min_lon, @microcosm.min_lat, @microcosm.max_lon, @microcosm.max_lat).to_scaled
Changeset
.where("min_lon < ? and max_lon > ? and min_lat < ? and max_lat > ?",
bbox.max_lon.to_i,
bbox.min_lon.to_i,
bbox.max_lat.to_i,
bbox.min_lat.to_i)
.order("changesets.id DESC").limit(20).preload(:user, :changeset_tags, :comments)
end
def step_up
message = nil
if @microcosm.organizers.empty?
if @microcosm.member?(current_user)
message = t ".you_have_stepped_up"
add_first_organizer
else
message = t ".only_members_can_step_up"
end
else
message = t ".already_has_organizer"
end
# render :show
redirect_to @microcosm, :notice => message
end
private
def set_microcosm
@microcosm = Microcosm.friendly.find(params[:id])
end
def microcosm_params
params.require(:microcosm).permit(
:name, :location, :latitude, :longitude,
:min_lat, :max_lat, :min_lon, :max_lon,
:description
)
end
end