Basic donation records
Adds donation model/table and basic manual management in the admin panel, as well as basic listing of users' own donations.
This commit is contained in:
parent
f3d6e29e4e
commit
40f3e8327a
@ -22,10 +22,6 @@ form {
|
||||
color: #bc0101;
|
||||
}
|
||||
|
||||
.actions {
|
||||
margin-top: 2rem;
|
||||
}
|
||||
|
||||
.accept-terms {
|
||||
margin-top: 2rem;
|
||||
font-size: 0.85rem;
|
||||
|
@ -143,6 +143,10 @@ main {
|
||||
margin-bottom: 3rem;
|
||||
}
|
||||
}
|
||||
|
||||
.actions {
|
||||
margin-top: 2rem;
|
||||
}
|
||||
}
|
||||
|
||||
.grid {
|
||||
|
74
app/controllers/admin/donations_controller.rb
Normal file
74
app/controllers/admin/donations_controller.rb
Normal file
@ -0,0 +1,74 @@
|
||||
class Admin::DonationsController < Admin::BaseController
|
||||
before_action :set_donation, only: [:show, :edit, :update, :destroy]
|
||||
|
||||
# GET /donations
|
||||
# GET /donations.json
|
||||
def index
|
||||
@donations = Donation.all
|
||||
end
|
||||
|
||||
# GET /donations/1
|
||||
# GET /donations/1.json
|
||||
def show
|
||||
end
|
||||
|
||||
# GET /donations/new
|
||||
def new
|
||||
@donation = Donation.new
|
||||
end
|
||||
|
||||
# GET /donations/1/edit
|
||||
def edit
|
||||
end
|
||||
|
||||
# POST /donations
|
||||
# POST /donations.json
|
||||
def create
|
||||
@donation = Donation.new(donation_params)
|
||||
|
||||
respond_to do |format|
|
||||
if @donation.save
|
||||
format.html { redirect_to admin_donation_url(@donation), notice: 'Donation was successfully created.' }
|
||||
format.json { render :show, status: :created, location: @donation }
|
||||
else
|
||||
format.html { render :new }
|
||||
format.json { render json: @donation.errors, status: :unprocessable_entity }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# PATCH/PUT /donations/1
|
||||
# PATCH/PUT /donations/1.json
|
||||
def update
|
||||
respond_to do |format|
|
||||
if @donation.update(donation_params)
|
||||
format.html { redirect_to admin_donation_url(@donation), notice: 'Donation was successfully updated.' }
|
||||
format.json { render :show, status: :ok, location: @donation }
|
||||
else
|
||||
format.html { render :edit }
|
||||
format.json { render json: @donation.errors, status: :unprocessable_entity }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# DELETE /donations/1
|
||||
# DELETE /donations/1.json
|
||||
def destroy
|
||||
@donation.destroy
|
||||
respond_to do |format|
|
||||
format.html { redirect_to admin_donations_url, notice: 'Donation was successfully destroyed.' }
|
||||
format.json { head :no_content }
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
# Use callbacks to share common setup or constraints between actions.
|
||||
def set_donation
|
||||
@donation = Donation.find(params[:id])
|
||||
end
|
||||
|
||||
# Only allow a list of trusted parameters through.
|
||||
def donation_params
|
||||
params.require(:donation).permit(:user_id, :amount_sats, :amount_eur, :amount_usd, :public_name)
|
||||
end
|
||||
end
|
9
app/controllers/donations_controller.rb
Normal file
9
app/controllers/donations_controller.rb
Normal file
@ -0,0 +1,9 @@
|
||||
class DonationsController < ApplicationController
|
||||
before_action :require_user_signed_in
|
||||
|
||||
# GET /donations
|
||||
# GET /donations.json
|
||||
def index
|
||||
@donations = current_user.donations
|
||||
end
|
||||
end
|
2
app/helpers/donations_helper.rb
Normal file
2
app/helpers/donations_helper.rb
Normal file
@ -0,0 +1,2 @@
|
||||
module DonationsHelper
|
||||
end
|
10
app/models/donation.rb
Normal file
10
app/models/donation.rb
Normal file
@ -0,0 +1,10 @@
|
||||
class Donation < ApplicationRecord
|
||||
# Relations
|
||||
belongs_to :user
|
||||
|
||||
# Validations
|
||||
validates_presence_of :amount_sats
|
||||
|
||||
# Hooks
|
||||
# TODO before_create :store_fiat_value
|
||||
end
|
@ -3,6 +3,7 @@ class User < ApplicationRecord
|
||||
|
||||
# Relations
|
||||
has_many :invitations, dependent: :destroy
|
||||
has_many :donations
|
||||
|
||||
validates_uniqueness_of :cn
|
||||
validates_length_of :cn, :minimum => 3
|
||||
|
@ -2,6 +2,7 @@
|
||||
<p>
|
||||
Ohai there, admin human.
|
||||
</p>
|
||||
<p>
|
||||
<%= link_to 'LDAP users', admin_ldap_users_path %>
|
||||
</p>
|
||||
<ul>
|
||||
<li><%= link_to 'LDAP users', admin_ldap_users_path %></li>
|
||||
<li><%= link_to 'Donations', admin_donations_path %></li>
|
||||
</ul>
|
||||
|
2
app/views/admin/donations/_donation.json.jbuilder
Normal file
2
app/views/admin/donations/_donation.json.jbuilder
Normal file
@ -0,0 +1,2 @@
|
||||
json.extract! donation, :id, :user_id, :amount_sats, :amount_eur, :amount_usd, :public_name, :created_at, :updated_at
|
||||
json.url donation_url(donation, format: :json)
|
53
app/views/admin/donations/_form.html.erb
Normal file
53
app/views/admin/donations/_form.html.erb
Normal file
@ -0,0 +1,53 @@
|
||||
<%= form_with(url: url, model: donation, local: true) do |form| %>
|
||||
<% if donation.errors.any? %>
|
||||
<div id="error_explanation">
|
||||
<h3><%= pluralize(donation.errors.count, "error") %> prohibited this donation from being saved:</h3>
|
||||
<ul>
|
||||
<% donation.errors.full_messages.each do |message| %>
|
||||
<li><%= message %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div class="field">
|
||||
<p>
|
||||
<%= form.label :user_id %>
|
||||
<%= form.collection_select :user_id, User.where(ou: "kosmos.org").order(:cn), :id, :cn %>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<p>
|
||||
<%= form.label :amount_sats, "Amount BTC (sats)" %>
|
||||
<%= form.number_field :amount_sats %>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<p>
|
||||
<%= form.label :amount_eur, "Amount EUR (cents)" %>
|
||||
<%= form.number_field :amount_eur %>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<p>
|
||||
<%= form.label :amount_usd, "Amount USD (cents)"%>
|
||||
<%= form.number_field :amount_usd %>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<p>
|
||||
<%= form.label :public_name %>
|
||||
<%= form.text_field :public_name %>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="actions">
|
||||
<p>
|
||||
<%= form.submit %>
|
||||
</p>
|
||||
</div>
|
||||
<% end %>
|
8
app/views/admin/donations/edit.html.erb
Normal file
8
app/views/admin/donations/edit.html.erb
Normal file
@ -0,0 +1,8 @@
|
||||
<h2>Editing Donation</h2>
|
||||
|
||||
<%= render 'form', donation: @donation, url: admin_donation_path(@donation) %>
|
||||
|
||||
<p class="actions">
|
||||
<%= link_to 'Show', admin_donation_path(@donation) %> |
|
||||
<%= link_to 'Back', admin_donations_path %>
|
||||
<p>
|
39
app/views/admin/donations/index.html.erb
Normal file
39
app/views/admin/donations/index.html.erb
Normal file
@ -0,0 +1,39 @@
|
||||
<h2>Donations</h2>
|
||||
|
||||
<% if @donations.any? %>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>User</th>
|
||||
<th>Amount BTC (sats)</th>
|
||||
<th>in EUR</th>
|
||||
<th>in USD</th>
|
||||
<th>Public name</th>
|
||||
<th colspan="3"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
<% @donations.each do |donation| %>
|
||||
<tr>
|
||||
<td><%= donation.user.cn %></td>
|
||||
<td><%= donation.amount_sats %></td>
|
||||
<td><%= donation.amount_eur %></td>
|
||||
<td><%= donation.amount_usd %></td>
|
||||
<td><%= donation.public_name %></td>
|
||||
<td><%= link_to 'Show', admin_donation_path(donation) %></td>
|
||||
<td><%= link_to 'Edit', edit_admin_donation_path(donation) %></td>
|
||||
<td><%= link_to 'Destroy', admin_donation_path(donation), method: :delete, data: { confirm: 'Are you sure?' } %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
<% else %>
|
||||
<p>
|
||||
No donations yet.
|
||||
</p>
|
||||
<% end %>
|
||||
|
||||
<p class="actions">
|
||||
<%= link_to 'Record an out-of-system donation', new_admin_donation_path %>
|
||||
</p>
|
1
app/views/admin/donations/index.json.jbuilder
Normal file
1
app/views/admin/donations/index.json.jbuilder
Normal file
@ -0,0 +1 @@
|
||||
json.array! @donations, partial: "donations/donation", as: :donation
|
7
app/views/admin/donations/new.html.erb
Normal file
7
app/views/admin/donations/new.html.erb
Normal file
@ -0,0 +1,7 @@
|
||||
<h2>New Donation</h2>
|
||||
|
||||
<%= render 'form', donation: @donation, url: admin_donations_path %>
|
||||
|
||||
<p class="actions">
|
||||
<%= link_to 'Back', admin_donations_path %>
|
||||
</p>
|
29
app/views/admin/donations/show.html.erb
Normal file
29
app/views/admin/donations/show.html.erb
Normal file
@ -0,0 +1,29 @@
|
||||
<p id="notice"><%= notice %></p>
|
||||
|
||||
<p>
|
||||
<strong>User:</strong>
|
||||
<%= @donation.user_id %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<strong>Amount sats:</strong>
|
||||
<%= @donation.amount_sats %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<strong>Amount eur:</strong>
|
||||
<%= @donation.amount_eur %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<strong>Amount usd:</strong>
|
||||
<%= @donation.amount_usd %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<strong>Public name:</strong>
|
||||
<%= @donation.public_name %>
|
||||
</p>
|
||||
|
||||
<%= link_to 'Edit', edit_admin_donation_path(@donation) %> |
|
||||
<%= link_to 'Back', admin_donations_path %>
|
1
app/views/admin/donations/show.json.jbuilder
Normal file
1
app/views/admin/donations/show.json.jbuilder
Normal file
@ -0,0 +1 @@
|
||||
json.partial! "donations/donation", donation: @donation
|
29
app/views/donations/index.html.erb
Normal file
29
app/views/donations/index.html.erb
Normal file
@ -0,0 +1,29 @@
|
||||
<h2>Donations</h2>
|
||||
|
||||
<% if @donations.any? %>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Amount BTC (sats)</th>
|
||||
<th>in EUR</th>
|
||||
<th>in USD</th>
|
||||
<th>Public name</th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
<% @donations.each do |donation| %>
|
||||
<tr>
|
||||
<td><%= donation.amount_sats %></td>
|
||||
<td><%= donation.amount_eur %></td>
|
||||
<td><%= donation.amount_usd %></td>
|
||||
<td><%= donation.public_name %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
<% else %>
|
||||
<p>
|
||||
No donations to show.
|
||||
</p>
|
||||
<% end %>
|
1
app/views/donations/index.json.jbuilder
Normal file
1
app/views/donations/index.json.jbuilder
Normal file
@ -0,0 +1 @@
|
||||
json.array! @donations, partial: "donations/donation", as: :donation
|
@ -1,4 +1,5 @@
|
||||
Rails.application.routes.draw do
|
||||
resources :donations
|
||||
devise_for :users
|
||||
|
||||
get 'welcome', to: 'welcome#index'
|
||||
@ -16,6 +17,7 @@ Rails.application.routes.draw do
|
||||
namespace :admin do
|
||||
root to: 'dashboard#index'
|
||||
get 'ldap_users', to: 'ldap_users#index'
|
||||
resources :donations
|
||||
end
|
||||
|
||||
# Letter Opener (open "sent" emails in dev and staging)
|
||||
|
@ -8,6 +8,7 @@ class CreateInvitations < ActiveRecord::Migration[6.0]
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
add_index :invitations, :user_id
|
||||
add_index :invitations, :invited_user_id
|
||||
end
|
||||
|
15
db/migrate/20201217161544_create_donations.rb
Normal file
15
db/migrate/20201217161544_create_donations.rb
Normal file
@ -0,0 +1,15 @@
|
||||
class CreateDonations < ActiveRecord::Migration[6.0]
|
||||
def change
|
||||
create_table :donations do |t|
|
||||
t.integer :user_id
|
||||
t.integer :amount_sats
|
||||
t.integer :amount_eur
|
||||
t.integer :amount_usd
|
||||
t.string :public_name
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
add_index :donations, :user_id
|
||||
end
|
||||
end
|
13
db/schema.rb
13
db/schema.rb
@ -10,7 +10,18 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 2020_11_30_132533) do
|
||||
ActiveRecord::Schema.define(version: 2020_12_17_161544) do
|
||||
|
||||
create_table "donations", force: :cascade do |t|
|
||||
t.integer "user_id"
|
||||
t.integer "amount_sats"
|
||||
t.integer "amount_eur"
|
||||
t.integer "amount_usd"
|
||||
t.string "public_name"
|
||||
t.datetime "created_at", precision: 6, null: false
|
||||
t.datetime "updated_at", precision: 6, null: false
|
||||
t.index ["user_id"], name: "index_donations_on_user_id"
|
||||
end
|
||||
|
||||
create_table "invitations", force: :cascade do |t|
|
||||
t.string "token"
|
||||
|
9
spec/factories/donations.rb
Normal file
9
spec/factories/donations.rb
Normal file
@ -0,0 +1,9 @@
|
||||
FactoryBot.define do
|
||||
factory :donation do
|
||||
user_id { 1 }
|
||||
amount_sats { 100000 }
|
||||
amount_eur { 10 }
|
||||
amount_usd { 13 }
|
||||
public_name { nil }
|
||||
end
|
||||
end
|
15
spec/helpers/donations_helper_spec.rb
Normal file
15
spec/helpers/donations_helper_spec.rb
Normal file
@ -0,0 +1,15 @@
|
||||
require 'rails_helper'
|
||||
|
||||
# Specs in this file have access to a helper object that includes
|
||||
# the DonationsHelper. For example:
|
||||
#
|
||||
# describe DonationsHelper do
|
||||
# describe "string concat" do
|
||||
# it "concats two strings with spaces" do
|
||||
# expect(helper.concat_strings("this","that")).to eq("this that")
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
RSpec.describe DonationsHelper, type: :helper do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
5
spec/models/donation_spec.rb
Normal file
5
spec/models/donation_spec.rb
Normal file
@ -0,0 +1,5 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Donation, type: :model do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user