diff options
author | Matthew Lemon <y@yulqen.org> | 2023-12-20 20:34:25 +0000 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2023-12-20 20:34:25 +0000 |
commit | 6b1a1834ad1715145f047790c8391b8a5558bece (patch) | |
tree | e20cdddb20477b56a013eb3bc357b9b5ff0488a3 /app | |
parent | a3e98876e2f88b69fef2a9da7d65b3704b6bf0d2 (diff) |
Created new Operation model and fixed Event associations with it
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/operations_controller.rb | 70 | ||||
-rw-r--r-- | app/helpers/operations_helper.rb | 2 | ||||
-rw-r--r-- | app/models/event.rb | 2 | ||||
-rw-r--r-- | app/models/operation.rb | 6 | ||||
-rw-r--r-- | app/models/organisation.rb | 2 | ||||
-rw-r--r-- | app/views/operations/_form.html.erb | 22 | ||||
-rw-r--r-- | app/views/operations/_operation.html.erb | 7 | ||||
-rw-r--r-- | app/views/operations/_operation.json.jbuilder | 2 | ||||
-rw-r--r-- | app/views/operations/edit.html.erb | 10 | ||||
-rw-r--r-- | app/views/operations/index.html.erb | 14 | ||||
-rw-r--r-- | app/views/operations/index.json.jbuilder | 1 | ||||
-rw-r--r-- | app/views/operations/new.html.erb | 9 | ||||
-rw-r--r-- | app/views/operations/show.html.erb | 10 | ||||
-rw-r--r-- | app/views/operations/show.json.jbuilder | 1 |
14 files changed, 156 insertions, 2 deletions
diff --git a/app/controllers/operations_controller.rb b/app/controllers/operations_controller.rb new file mode 100644 index 0000000..5081932 --- /dev/null +++ b/app/controllers/operations_controller.rb @@ -0,0 +1,70 @@ +class OperationsController < ApplicationController + before_action :set_operation, only: %i[ show edit update destroy ] + + # GET /operations or /operations.json + def index + @operations = Operation.all + end + + # GET /operations/1 or /operations/1.json + def show + end + + # GET /operations/new + def new + @operation = Operation.new + end + + # GET /operations/1/edit + def edit + end + + # POST /operations or /operations.json + def create + @operation = Operation.new(operation_params) + + respond_to do |format| + if @operation.save + format.html { redirect_to operation_url(@operation), notice: "Operation was successfully created." } + format.json { render :show, status: :created, location: @operation } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @operation.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /operations/1 or /operations/1.json + def update + respond_to do |format| + if @operation.update(operation_params) + format.html { redirect_to operation_url(@operation), notice: "Operation was successfully updated." } + format.json { render :show, status: :ok, location: @operation } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @operation.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /operations/1 or /operations/1.json + def destroy + @operation.destroy! + + respond_to do |format| + format.html { redirect_to operations_url, notice: "Operation was successfully destroyed." } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_operation + @operation = Operation.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def operation_params + params.require(:operation).permit(:name) + end +end diff --git a/app/helpers/operations_helper.rb b/app/helpers/operations_helper.rb new file mode 100644 index 0000000..d114c7c --- /dev/null +++ b/app/helpers/operations_helper.rb @@ -0,0 +1,2 @@ +module OperationsHelper +end diff --git a/app/models/event.rb b/app/models/event.rb index aaa4e6e..27f9aec 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -2,7 +2,7 @@ class Event < ApplicationRecord validates :name, presence: true, length: { maximum: 50 } validates :date, presence: true - belongs_to :organisation, optional: true + belongs_to :operation, optional: true def month_year_check(year, month) if self.date.year == year && self.date.month == month diff --git a/app/models/operation.rb b/app/models/operation.rb new file mode 100644 index 0000000..5506ec3 --- /dev/null +++ b/app/models/operation.rb @@ -0,0 +1,6 @@ +class Operation < ApplicationRecord + validates :name, presence: true, length: { maximum: 35 } + + has_many :events + belongs_to :organisation +end diff --git a/app/models/organisation.rb b/app/models/organisation.rb index 4177c35..cda399f 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -1,5 +1,5 @@ class Organisation < ApplicationRecord - has_many :events, dependent: :destroy + has_many :operations, dependent: :destroy validates :name, presence: true, length: { maximum: 25 } end diff --git a/app/views/operations/_form.html.erb b/app/views/operations/_form.html.erb new file mode 100644 index 0000000..adadad8 --- /dev/null +++ b/app/views/operations/_form.html.erb @@ -0,0 +1,22 @@ +<%= form_with(model: operation) do |form| %> + <% if operation.errors.any? %> + <div style="color: red"> + <h2><%= pluralize(operation.errors.count, "error") %> prohibited this operation from being saved:</h2> + + <ul> + <% operation.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/operations/_operation.html.erb b/app/views/operations/_operation.html.erb new file mode 100644 index 0000000..6349848 --- /dev/null +++ b/app/views/operations/_operation.html.erb @@ -0,0 +1,7 @@ +<div id="<%= dom_id operation %>"> + <p> + <strong>Name:</strong> + <%= operation.name %> + </p> + +</div> diff --git a/app/views/operations/_operation.json.jbuilder b/app/views/operations/_operation.json.jbuilder new file mode 100644 index 0000000..8016989 --- /dev/null +++ b/app/views/operations/_operation.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! operation, :id, :name, :created_at, :updated_at +json.url operation_url(operation, format: :json) diff --git a/app/views/operations/edit.html.erb b/app/views/operations/edit.html.erb new file mode 100644 index 0000000..650350a --- /dev/null +++ b/app/views/operations/edit.html.erb @@ -0,0 +1,10 @@ +<h1>Editing operation</h1> + +<%= render "form", operation: @operation %> + +<br> + +<div> + <%= link_to "Show this operation", @operation %> | + <%= link_to "Back to operations", operations_path %> +</div> diff --git a/app/views/operations/index.html.erb b/app/views/operations/index.html.erb new file mode 100644 index 0000000..8bf9009 --- /dev/null +++ b/app/views/operations/index.html.erb @@ -0,0 +1,14 @@ +<p style="color: green"><%= notice %></p> + +<h1>Operations</h1> + +<div id="operations"> + <% @operations.each do |operation| %> + <%= render operation %> + <p> + <%= link_to "Show this operation", operation %> + </p> + <% end %> +</div> + +<%= link_to "New operation", new_operation_path %> diff --git a/app/views/operations/index.json.jbuilder b/app/views/operations/index.json.jbuilder new file mode 100644 index 0000000..bef1085 --- /dev/null +++ b/app/views/operations/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @operations, partial: "operations/operation", as: :operation diff --git a/app/views/operations/new.html.erb b/app/views/operations/new.html.erb new file mode 100644 index 0000000..be6230d --- /dev/null +++ b/app/views/operations/new.html.erb @@ -0,0 +1,9 @@ +<h1>New operation</h1> + +<%= render "form", operation: @operation %> + +<br> + +<div> + <%= link_to "Back to operations", operations_path %> +</div> diff --git a/app/views/operations/show.html.erb b/app/views/operations/show.html.erb new file mode 100644 index 0000000..d19705b --- /dev/null +++ b/app/views/operations/show.html.erb @@ -0,0 +1,10 @@ +<p style="color: green"><%= notice %></p> + +<%= render @operation %> + +<div> + <%= link_to "Edit this operation", edit_operation_path(@operation) %> | + <%= link_to "Back to operations", operations_path %> + + <%= button_to "Destroy this operation", @operation, method: :delete %> +</div> diff --git a/app/views/operations/show.json.jbuilder b/app/views/operations/show.json.jbuilder new file mode 100644 index 0000000..ada5250 --- /dev/null +++ b/app/views/operations/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "operations/operation", operation: @operation |