diff --git a/app/assets/stylesheets/donations.scss b/app/assets/stylesheets/donations.scss new file mode 100644 index 0000000..ef401ab --- /dev/null +++ b/app/assets/stylesheets/donations.scss @@ -0,0 +1,40 @@ +ul.donations { + list-style: none; + + li { + margin-bottom: 2rem; + display: grid; + grid-row-gap: 0.5rem; + grid-column-gap: 2rem; + grid-template-columns: 1fr 1fr; + grid-template-areas: + "date amount-btc" + "public-name amounts-fiat"; + + h3 { + grid-area: "date"; + margin-bottom: 0; + } + + p { + margin-bottom: 0; + + &.amount-btc { + grid-area: amount-btc; + text-align: right; + font-family: monospace; + font-size: 1.25rem; + } + &.amounts-fiat { + grid-area: amounts-fiat; + text-align: right; + font-family: monospace; + font-size: 0.85rem; + color: #888; + } + &.public-name { + grid-area: public-name; + } + } + } +} 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..7b00bac --- /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, :paid_at) + end +end diff --git a/app/controllers/donations_controller.rb b/app/controllers/donations_controller.rb new file mode 100644 index 0000000..74ee81c --- /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.completed + end +end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index de6be79..e2ff699 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,2 +1,5 @@ module ApplicationHelper + def sats_to_btc(sats) + sats.to_f / 100000000 + 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..30f13a0 --- /dev/null +++ b/app/models/donation.rb @@ -0,0 +1,13 @@ +class Donation < ApplicationRecord + # Relations + belongs_to :user + + # Validations + validates_presence_of :amount_sats + + # Hooks + # TODO before_create :store_fiat_value + + #Scopes + scope :completed, -> { where.not(paid_at: nil) } +end diff --git a/app/models/user.rb b/app/models/user.rb index 6987a0a..046b948 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, dependent: :nullify 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..038d129 --- /dev/null +++ b/app/views/admin/donations/_form.html.erb @@ -0,0 +1,60 @@ +<%= 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.label :paid_at %> + <%= form.text_field :paid_at %> +

+
+ +
+

+ <%= 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..e0d38ce --- /dev/null +++ b/app/views/admin/donations/index.html.erb @@ -0,0 +1,41 @@ +

Donations

+ +<% if @donations.any? %> + + + + + + + + + + + + + + + <% @donations.each do |donation| %> + + + + + + + + + + + + <% end %> + +
UserAmount BTCin EURin USDPublic nameDate
<%= donation.user.cn %><%= sats_to_btc donation.amount_sats %> BTC<%= number_to_currency donation.amount_eur / 100, unit: "" %><%= number_to_currency donation.amount_usd / 100, unit: "" %><%= donation.public_name %><%= donation.paid_at ? donation.paid_at.strftime("%Y-%m-%d") : "" %><%= 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..12bc37e --- /dev/null +++ b/app/views/admin/donations/show.html.erb @@ -0,0 +1,34 @@ +

<%= 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 %> +

+ +

+ Date: + <%= @donation.paid_at %> +

+ +<%= 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..7c504de --- /dev/null +++ b/app/views/donations/index.html.erb @@ -0,0 +1,38 @@ +
+

Donations

+

+ Your financial contributions to the development and + upkeep of Kosmos software and services. +

+
+ +
+ <% if @donations.any? %> + + <% 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/app/views/invitations/index.html.erb b/app/views/invitations/index.html.erb index 5b461ce..c38b98d 100644 --- a/app/views/invitations/index.html.erb +++ b/app/views/invitations/index.html.erb @@ -29,7 +29,7 @@ URL - Used at + Accepted Invited user @@ -37,7 +37,7 @@ <% @invitations_used.each do |invitation| %> <%= invitation_url(invitation.token) %> - <%= invitation.used_at %> + <%= invitation.used_at.strftime("%Y-%m-%d") %> <%= User.find(invitation.invited_user_id).address %> <% end %> 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/migrate/20201219121808_add_paid_at_to_donations.rb b/db/migrate/20201219121808_add_paid_at_to_donations.rb new file mode 100644 index 0000000..ce690c5 --- /dev/null +++ b/db/migrate/20201219121808_add_paid_at_to_donations.rb @@ -0,0 +1,5 @@ +class AddPaidAtToDonations < ActiveRecord::Migration[6.0] + def change + add_column :donations, :paid_at, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index 3d99780..db024da 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,19 @@ # # 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_19_121808) 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.datetime "paid_at" + 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/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb new file mode 100644 index 0000000..92f8de4 --- /dev/null +++ b/spec/helpers/application_helper_spec.rb @@ -0,0 +1,9 @@ +require 'rails_helper' + +describe ApplicationHelper do + describe "sats_to_btc" do + it "converts satoshis to BTC" do + expect(helper.sats_to_btc(120000000)).to eq(1.2) + end + 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