aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/controllers/events_controller.rb2
-rw-r--r--app/controllers/organisations_controller.rb70
-rw-r--r--app/helpers/organisations_helper.rb2
-rw-r--r--app/models/event.rb2
-rw-r--r--app/models/organisation.rb3
-rw-r--r--app/views/events/_form.html.erb5
-rw-r--r--app/views/organisations/_form.html.erb22
-rw-r--r--app/views/organisations/_organisation.html.erb7
-rw-r--r--app/views/organisations/_organisation.json.jbuilder2
-rw-r--r--app/views/organisations/edit.html.erb10
-rw-r--r--app/views/organisations/index.html.erb14
-rw-r--r--app/views/organisations/index.json.jbuilder1
-rw-r--r--app/views/organisations/new.html.erb9
-rw-r--r--app/views/organisations/show.html.erb10
-rw-r--r--app/views/organisations/show.json.jbuilder1
15 files changed, 159 insertions, 1 deletions
diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb
index 058486d..d819632 100644
--- a/app/controllers/events_controller.rb
+++ b/app/controllers/events_controller.rb
@@ -67,6 +67,6 @@ class EventsController < ApplicationController
# Only allow a list of trusted parameters through.
def event_params
- params.require(:event).permit(:date, :name)
+ params.require(:event).permit(:date, :name, :organisation_id)
end
end
diff --git a/app/controllers/organisations_controller.rb b/app/controllers/organisations_controller.rb
new file mode 100644
index 0000000..dbff777
--- /dev/null
+++ b/app/controllers/organisations_controller.rb
@@ -0,0 +1,70 @@
+class OrganisationsController < ApplicationController
+ before_action :set_organisation, only: %i[ show edit update destroy ]
+
+ # GET /organisations or /organisations.json
+ def index
+ @organisations = Organisation.all
+ end
+
+ # GET /organisations/1 or /organisations/1.json
+ def show
+ end
+
+ # GET /organisations/new
+ def new
+ @organisation = Organisation.new
+ end
+
+ # GET /organisations/1/edit
+ def edit
+ end
+
+ # POST /organisations or /organisations.json
+ def create
+ @organisation = Organisation.new(organisation_params)
+
+ respond_to do |format|
+ if @organisation.save
+ format.html { redirect_to organisation_url(@organisation), notice: "Organisation was successfully created." }
+ format.json { render :show, status: :created, location: @organisation }
+ else
+ format.html { render :new, status: :unprocessable_entity }
+ format.json { render json: @organisation.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # PATCH/PUT /organisations/1 or /organisations/1.json
+ def update
+ respond_to do |format|
+ if @organisation.update(organisation_params)
+ format.html { redirect_to organisation_url(@organisation), notice: "Organisation was successfully updated." }
+ format.json { render :show, status: :ok, location: @organisation }
+ else
+ format.html { render :edit, status: :unprocessable_entity }
+ format.json { render json: @organisation.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /organisations/1 or /organisations/1.json
+ def destroy
+ @organisation.destroy!
+
+ respond_to do |format|
+ format.html { redirect_to organisations_url, notice: "Organisation was successfully destroyed." }
+ format.json { head :no_content }
+ end
+ end
+
+ private
+ # Use callbacks to share common setup or constraints between actions.
+ def set_organisation
+ @organisation = Organisation.find(params[:id])
+ end
+
+ # Only allow a list of trusted parameters through.
+ def organisation_params
+ params.require(:organisation).permit(:name)
+ end
+end
diff --git a/app/helpers/organisations_helper.rb b/app/helpers/organisations_helper.rb
new file mode 100644
index 0000000..1b84554
--- /dev/null
+++ b/app/helpers/organisations_helper.rb
@@ -0,0 +1,2 @@
+module OrganisationsHelper
+end
diff --git a/app/models/event.rb b/app/models/event.rb
index 9dea7b8..aaa4e6e 100644
--- a/app/models/event.rb
+++ b/app/models/event.rb
@@ -2,6 +2,8 @@ class Event < ApplicationRecord
validates :name, presence: true, length: { maximum: 50 }
validates :date, presence: true
+ belongs_to :organisation, optional: true
+
def month_year_check(year, month)
if self.date.year == year && self.date.month == month
true
diff --git a/app/models/organisation.rb b/app/models/organisation.rb
new file mode 100644
index 0000000..aa67564
--- /dev/null
+++ b/app/models/organisation.rb
@@ -0,0 +1,3 @@
+class Organisation < ApplicationRecord
+ has_many :events
+end
diff --git a/app/views/events/_form.html.erb b/app/views/events/_form.html.erb
index 2a41eec..98dd972 100644
--- a/app/views/events/_form.html.erb
+++ b/app/views/events/_form.html.erb
@@ -36,6 +36,11 @@
<%= form.label "Event name", style: "display: block" %>
<%= form.text_field :name %>
</div>
+
+ <div>
+ <%= form.label "Organisation", style: "display: block" %>
+ <%= form.select :organisation, Organisation.all.collect { |o| [ o.name, o.id ] }, include_blank: true %>
+ </div>
<div>
<%= form.submit style: "background: green; color: white; margin-top: 1.1em;"%>
diff --git a/app/views/organisations/_form.html.erb b/app/views/organisations/_form.html.erb
new file mode 100644
index 0000000..ebe8ea3
--- /dev/null
+++ b/app/views/organisations/_form.html.erb
@@ -0,0 +1,22 @@
+<%= form_with(model: organisation) do |form| %>
+ <% if organisation.errors.any? %>
+ <div style="color: red">
+ <h2><%= pluralize(organisation.errors.count, "error") %> prohibited this organisation from being saved:</h2>
+
+ <ul>
+ <% organisation.errors.each do |error| %>
+ <li><%= error.full_message %></li>
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
+
+ <div>
+ <%= form.label :name, style: "display: block" %>
+ <%= form.text_field :name %>
+ </div>
+
+ <div>
+ <%= form.submit %>
+ </div>
+<% end %>
diff --git a/app/views/organisations/_organisation.html.erb b/app/views/organisations/_organisation.html.erb
new file mode 100644
index 0000000..c5ea553
--- /dev/null
+++ b/app/views/organisations/_organisation.html.erb
@@ -0,0 +1,7 @@
+<div id="<%= dom_id organisation %>">
+ <p>
+ <strong>Name:</strong>
+ <%= organisation.name %>
+ </p>
+
+</div>
diff --git a/app/views/organisations/_organisation.json.jbuilder b/app/views/organisations/_organisation.json.jbuilder
new file mode 100644
index 0000000..e4d8f92
--- /dev/null
+++ b/app/views/organisations/_organisation.json.jbuilder
@@ -0,0 +1,2 @@
+json.extract! organisation, :id, :name, :created_at, :updated_at
+json.url organisation_url(organisation, format: :json)
diff --git a/app/views/organisations/edit.html.erb b/app/views/organisations/edit.html.erb
new file mode 100644
index 0000000..95bea56
--- /dev/null
+++ b/app/views/organisations/edit.html.erb
@@ -0,0 +1,10 @@
+<h1>Editing organisation</h1>
+
+<%= render "form", organisation: @organisation %>
+
+<br>
+
+<div>
+ <%= link_to "Show this organisation", @organisation %> |
+ <%= link_to "Back to organisations", organisations_path %>
+</div>
diff --git a/app/views/organisations/index.html.erb b/app/views/organisations/index.html.erb
new file mode 100644
index 0000000..45d5b16
--- /dev/null
+++ b/app/views/organisations/index.html.erb
@@ -0,0 +1,14 @@
+<p style="color: green"><%= notice %></p>
+
+<h1>Organisations</h1>
+
+<div id="organisations">
+ <% @organisations.each do |organisation| %>
+ <%= render organisation %>
+ <p>
+ <%= link_to "Show this organisation", organisation %>
+ </p>
+ <% end %>
+</div>
+
+<%= link_to "New organisation", new_organisation_path %>
diff --git a/app/views/organisations/index.json.jbuilder b/app/views/organisations/index.json.jbuilder
new file mode 100644
index 0000000..4490dc6
--- /dev/null
+++ b/app/views/organisations/index.json.jbuilder
@@ -0,0 +1 @@
+json.array! @organisations, partial: "organisations/organisation", as: :organisation
diff --git a/app/views/organisations/new.html.erb b/app/views/organisations/new.html.erb
new file mode 100644
index 0000000..a749bc8
--- /dev/null
+++ b/app/views/organisations/new.html.erb
@@ -0,0 +1,9 @@
+<h1>New organisation</h1>
+
+<%= render "form", organisation: @organisation %>
+
+<br>
+
+<div>
+ <%= link_to "Back to organisations", organisations_path %>
+</div>
diff --git a/app/views/organisations/show.html.erb b/app/views/organisations/show.html.erb
new file mode 100644
index 0000000..3de432b
--- /dev/null
+++ b/app/views/organisations/show.html.erb
@@ -0,0 +1,10 @@
+<p style="color: green"><%= notice %></p>
+
+<%= render @organisation %>
+
+<div>
+ <%= link_to "Edit this organisation", edit_organisation_path(@organisation) %> |
+ <%= link_to "Back to organisations", organisations_path %>
+
+ <%= button_to "Destroy this organisation", @organisation, method: :delete %>
+</div>
diff --git a/app/views/organisations/show.json.jbuilder b/app/views/organisations/show.json.jbuilder
new file mode 100644
index 0000000..c228d36
--- /dev/null
+++ b/app/views/organisations/show.json.jbuilder
@@ -0,0 +1 @@
+json.partial! "organisations/organisation", organisation: @organisation