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 %> -
++ <%= 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 %> +
++ <%= 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 @@ +
User | +Amount BTC | +in EUR | +in USD | +Public name | +Date | ++ | ||
---|---|---|---|---|---|---|---|---|
<%= 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?' } %> | +
+ 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 @@ ++ <%= 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 @@ ++ Your financial contributions to the development and + upkeep of Kosmos software and services. +
++ <%= sats_to_btc donation.amount_sats %> BTC +
++ (~ <%= number_to_currency donation.amount_eur / 100, unit: "" %> EUR) +
++ <% if donation.public_name.present? %> + Public name: <%= donation.public_name %> + <% else %> + Anonymous + <% end %> +
++ No donations to show. +
+ <% end %> +