diff --git a/app/assets/stylesheets/forms.scss b/app/assets/stylesheets/forms.scss index e8afb30..b994c05 100644 --- a/app/assets/stylesheets/forms.scss +++ b/app/assets/stylesheets/forms.scss @@ -22,10 +22,6 @@ form { color: #bc0101; } - .actions { - margin-top: 2rem; - } - .accept-terms { margin-top: 2rem; font-size: 0.85rem; diff --git a/app/assets/stylesheets/layout.scss b/app/assets/stylesheets/layout.scss index f57e8e5..c615505 100644 --- a/app/assets/stylesheets/layout.scss +++ b/app/assets/stylesheets/layout.scss @@ -143,6 +143,10 @@ main { margin-bottom: 3rem; } } + + .actions { + margin-top: 2rem; + } } .grid { diff --git a/app/controllers/admin/donations_controller.rb b/app/controllers/admin/donations_controller.rb new file mode 100644 index 0000000..6aa0b36 --- /dev/null +++ b/app/controllers/admin/donations_controller.rb @@ -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 diff --git a/app/controllers/donations_controller.rb b/app/controllers/donations_controller.rb new file mode 100644 index 0000000..d5d74bc --- /dev/null +++ b/app/controllers/donations_controller.rb @@ -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 diff --git a/app/helpers/donations_helper.rb b/app/helpers/donations_helper.rb new file mode 100644 index 0000000..d04dc2d --- /dev/null +++ b/app/helpers/donations_helper.rb @@ -0,0 +1,2 @@ +module DonationsHelper +end diff --git a/app/models/donation.rb b/app/models/donation.rb new file mode 100644 index 0000000..183e7fc --- /dev/null +++ b/app/models/donation.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 6987a0a..da3a01a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/views/admin/dashboard/index.html.erb b/app/views/admin/dashboard/index.html.erb index d718467..34a898c 100644 --- a/app/views/admin/dashboard/index.html.erb +++ b/app/views/admin/dashboard/index.html.erb @@ -2,6 +2,7 @@

Ohai there, admin human.

-

- <%= link_to 'LDAP users', admin_ldap_users_path %> -

+ diff --git a/app/views/admin/donations/_donation.json.jbuilder b/app/views/admin/donations/_donation.json.jbuilder new file mode 100644 index 0000000..51574aa --- /dev/null +++ b/app/views/admin/donations/_donation.json.jbuilder @@ -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) diff --git a/app/views/admin/donations/_form.html.erb b/app/views/admin/donations/_form.html.erb new file mode 100644 index 0000000..09f124c --- /dev/null +++ b/app/views/admin/donations/_form.html.erb @@ -0,0 +1,53 @@ +<%= form_with(url: url, model: donation, local: true) do |form| %> + <% if donation.errors.any? %> +
+

<%= pluralize(donation.errors.count, "error") %> prohibited this donation from being saved:

+ +
+ <% end %> + +
+

+ <%= form.label :user_id %> + <%= form.collection_select :user_id, User.where(ou: "kosmos.org").order(:cn), :id, :cn %> +

+
+ +
+

+ <%= form.label :amount_sats, "Amount BTC (sats)" %> + <%= form.number_field :amount_sats %> +

+
+ +
+

+ <%= form.label :amount_eur, "Amount EUR (cents)" %> + <%= form.number_field :amount_eur %> +

+
+ +
+

+ <%= form.label :amount_usd, "Amount USD (cents)"%> + <%= form.number_field :amount_usd %> +

+
+ +
+

+ <%= form.label :public_name %> + <%= form.text_field :public_name %> +

+
+ +
+

+ <%= form.submit %> +

+
+<% end %> diff --git a/app/views/admin/donations/edit.html.erb b/app/views/admin/donations/edit.html.erb new file mode 100644 index 0000000..f3bffd3 --- /dev/null +++ b/app/views/admin/donations/edit.html.erb @@ -0,0 +1,8 @@ +

Editing Donation

+ +<%= render 'form', donation: @donation, url: admin_donation_path(@donation) %> + +

+ <%= link_to 'Show', admin_donation_path(@donation) %> | + <%= link_to 'Back', admin_donations_path %> +

diff --git a/app/views/admin/donations/index.html.erb b/app/views/admin/donations/index.html.erb new file mode 100644 index 0000000..ef69627 --- /dev/null +++ b/app/views/admin/donations/index.html.erb @@ -0,0 +1,39 @@ +

Donations

+ +<% if @donations.any? %> + + + + + + + + + + + + + + <% @donations.each do |donation| %> + + + + + + + + + + + <% end %> + +
UserAmount BTC (sats)in EURin USDPublic name
<%= donation.user.cn %><%= donation.amount_sats %><%= donation.amount_eur %><%= donation.amount_usd %><%= donation.public_name %><%= link_to 'Show', admin_donation_path(donation) %><%= link_to 'Edit', edit_admin_donation_path(donation) %><%= link_to 'Destroy', admin_donation_path(donation), method: :delete, data: { confirm: 'Are you sure?' } %>
+<% else %> +

+ No donations yet. +

+<% end %> + +

+ <%= link_to 'Record an out-of-system donation', new_admin_donation_path %> +

diff --git a/app/views/admin/donations/index.json.jbuilder b/app/views/admin/donations/index.json.jbuilder new file mode 100644 index 0000000..9df428c --- /dev/null +++ b/app/views/admin/donations/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @donations, partial: "donations/donation", as: :donation diff --git a/app/views/admin/donations/new.html.erb b/app/views/admin/donations/new.html.erb new file mode 100644 index 0000000..4fd44bf --- /dev/null +++ b/app/views/admin/donations/new.html.erb @@ -0,0 +1,7 @@ +

New Donation

+ +<%= render 'form', donation: @donation, url: admin_donations_path %> + +

+ <%= link_to 'Back', admin_donations_path %> +

diff --git a/app/views/admin/donations/show.html.erb b/app/views/admin/donations/show.html.erb new file mode 100644 index 0000000..b6bc85a --- /dev/null +++ b/app/views/admin/donations/show.html.erb @@ -0,0 +1,29 @@ +

<%= notice %>

+ +

+ User: + <%= @donation.user_id %> +

+ +

+ Amount sats: + <%= @donation.amount_sats %> +

+ +

+ Amount eur: + <%= @donation.amount_eur %> +

+ +

+ Amount usd: + <%= @donation.amount_usd %> +

+ +

+ Public name: + <%= @donation.public_name %> +

+ +<%= link_to 'Edit', edit_admin_donation_path(@donation) %> | +<%= link_to 'Back', admin_donations_path %> diff --git a/app/views/admin/donations/show.json.jbuilder b/app/views/admin/donations/show.json.jbuilder new file mode 100644 index 0000000..a152e38 --- /dev/null +++ b/app/views/admin/donations/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "donations/donation", donation: @donation diff --git a/app/views/donations/index.html.erb b/app/views/donations/index.html.erb new file mode 100644 index 0000000..67c2fe2 --- /dev/null +++ b/app/views/donations/index.html.erb @@ -0,0 +1,29 @@ +

Donations

+ +<% if @donations.any? %> + + + + + + + + + + + + <% @donations.each do |donation| %> + + + + + + + <% end %> + +
Amount BTC (sats)in EURin USDPublic name
<%= donation.amount_sats %><%= donation.amount_eur %><%= donation.amount_usd %><%= donation.public_name %>
+<% else %> +

+ No donations to show. +

+<% end %> diff --git a/app/views/donations/index.json.jbuilder b/app/views/donations/index.json.jbuilder new file mode 100644 index 0000000..9df428c --- /dev/null +++ b/app/views/donations/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @donations, partial: "donations/donation", as: :donation diff --git a/config/routes.rb b/config/routes.rb index bdefef0..5873f71 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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) diff --git a/db/migrate/20201130132533_create_invitations.rb b/db/migrate/20201130132533_create_invitations.rb index 0b53910..aea9997 100644 --- a/db/migrate/20201130132533_create_invitations.rb +++ b/db/migrate/20201130132533_create_invitations.rb @@ -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 diff --git a/db/migrate/20201217161544_create_donations.rb b/db/migrate/20201217161544_create_donations.rb new file mode 100644 index 0000000..8043a37 --- /dev/null +++ b/db/migrate/20201217161544_create_donations.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 3d99780..49ae73a 100644 --- a/db/schema.rb +++ b/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" diff --git a/spec/factories/donations.rb b/spec/factories/donations.rb new file mode 100644 index 0000000..cb45fd6 --- /dev/null +++ b/spec/factories/donations.rb @@ -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 diff --git a/spec/helpers/donations_helper_spec.rb b/spec/helpers/donations_helper_spec.rb new file mode 100644 index 0000000..1834826 --- /dev/null +++ b/spec/helpers/donations_helper_spec.rb @@ -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 diff --git a/spec/models/donation_spec.rb b/spec/models/donation_spec.rb new file mode 100644 index 0000000..b23ee4e --- /dev/null +++ b/spec/models/donation_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Donation, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end