discourse-subscriptions/plugin.rb

173 lines
5.1 KiB
Ruby
Raw Normal View History

2019-08-27 06:04:02 -04:00
# frozen_string_literal: true
2017-02-23 20:42:59 -05:00
# name: discourse-donations
# about: Integrates Stripe into Discourse to allow forum visitors to make donations
2019-08-27 05:09:24 -04:00
# version: 1.11.3
2019-08-26 02:01:57 -04:00
# url: https://github.com/rimian/discourse-donations
2018-06-25 04:14:50 -04:00
# authors: Rimian Perkins, Chris Beach, Angus McLeod
2017-01-30 21:28:41 -05:00
2017-04-30 20:48:40 -04:00
gem 'stripe', '2.8.0'
2019-04-02 21:40:03 -04:00
register_asset "stylesheets/common/discourse-donations.scss"
register_asset "stylesheets/mobile/discourse-donations.scss"
2017-02-27 21:36:41 -05:00
enabled_site_setting :discourse_donations_enabled
2017-11-11 22:16:47 -05:00
register_html_builder('server:before-head-close') do
"<script src='https://js.stripe.com/v3/'></script>"
end
2019-01-28 23:00:29 -05:00
extend_content_security_policy(
script_src: ['https://js.stripe.com/v3/']
)
after_initialize do
2018-06-25 04:14:50 -04:00
load File.expand_path('../lib/discourse_donations/engine.rb', __FILE__)
load File.expand_path('../config/routes.rb', __FILE__)
load File.expand_path('../app/controllers/controllers.rb', __FILE__)
2017-05-04 03:23:41 -04:00
load File.expand_path('../app/jobs/jobs.rb', __FILE__)
2018-06-25 04:14:50 -04:00
load File.expand_path('../app/services/services.rb', __FILE__)
Discourse::Application.routes.append do
mount ::DiscourseDonations::Engine, at: 'donate'
end
class ::User
def stripe_customer_id
if custom_fields['stripe_customer_id']
2018-06-21 05:00:43 -04:00
custom_fields['stripe_customer_id'].to_s
else
nil
end
end
end
2018-09-22 00:03:30 -04:00
Category.register_custom_field_type('donations_show_amounts', :boolean)
2018-09-22 00:03:30 -04:00
class ::Category
2018-09-22 03:09:06 -04:00
def donations_cause
SiteSetting.discourse_donations_causes_categories.split('|').include? self.id.to_s
end
2018-09-22 00:03:30 -04:00
def donations_total
if custom_fields['donations_total']
custom_fields['donations_total']
else
0
end
end
def donations_show_amounts
if custom_fields['donations_show_amounts'] != nil
custom_fields['donations_show_amounts']
else
2018-09-29 21:53:33 -04:00
false
end
end
2018-09-22 00:03:30 -04:00
def donations_month
if custom_fields['donations_month']
custom_fields['donations_month']
else
0
end
end
def donations_backers
if custom_fields['donations_backers']
[*custom_fields['donations_backers']].map do |user_id|
User.find_by(id: user_id.to_i)
end
else
[]
end
end
def donations_maintainers
if custom_fields['donations_maintainers']
custom_fields['donations_maintainers'].split(',').map do |username|
User.find_by(username: username)
end
else
[]
end
end
def donations_maintainers_label
if custom_fields['donations_maintainers_label']
custom_fields['donations_maintainers_label']
else
nil
end
end
2018-09-22 00:03:30 -04:00
def donations_github
if custom_fields['donations_github']
custom_fields['donations_github']
else
nil
2018-09-22 00:03:30 -04:00
end
end
2018-09-22 03:48:38 -04:00
def donations_meta
if custom_fields['donations_meta']
custom_fields['donations_meta']
else
nil
2018-09-22 03:48:38 -04:00
end
end
2019-04-02 21:32:24 -04:00
def donations_release_latest
if custom_fields['donations_release_latest']
custom_fields['donations_release_latest']
else
nil
end
end
def donations_release_oldest
if custom_fields['donations_release_oldest']
custom_fields['donations_release_oldest']
else
nil
end
end
2018-09-22 00:03:30 -04:00
end
2019-03-18 21:22:06 -04:00
[
'donations_cause',
'donations_total',
'donations_month',
'donations_backers',
'donations_show_amounts',
'donations_maintainers',
'donations_maintainers_label',
'donations_github',
2019-04-02 21:32:24 -04:00
'donations_meta',
'donations_release_latest',
'donations_release_oldest'
2019-03-18 21:22:06 -04:00
].each do |key|
Site.preloaded_category_custom_fields << key if Site.respond_to? :preloaded_category_custom_fields
end
add_to_serializer(:basic_category, :donations_cause) { object.donations_cause }
add_to_serializer(:basic_category, :donations_total) { object.donations_total }
add_to_serializer(:basic_category, :include_donations_total?) { object.donations_show_amounts }
add_to_serializer(:basic_category, :donations_month) { object.donations_month }
add_to_serializer(:basic_category, :include_donations_month?) { object.donations_show_amounts && SiteSetting.discourse_donations_cause_month }
add_to_serializer(:basic_category, :donations_backers) {
ActiveModel::ArraySerializer.new(object.donations_backers, each_serializer: BasicUserSerializer).as_json
}
add_to_serializer(:basic_category, :donations_maintainers) {
ActiveModel::ArraySerializer.new(object.donations_maintainers, each_serializer: BasicUserSerializer).as_json
}
add_to_serializer(:basic_category, :donations_maintainers_label) { object.donations_maintainers_label }
add_to_serializer(:basic_category, :include_donations_maintainers_label?) { object.donations_maintainers_label.present? }
add_to_serializer(:basic_category, :donations_github) { object.donations_github }
add_to_serializer(:basic_category, :donations_meta) { object.donations_meta }
2019-04-02 21:32:24 -04:00
add_to_serializer(:basic_category, :donations_release_latest) { object.donations_release_latest }
add_to_serializer(:basic_category, :donations_release_oldest) { object.donations_release_oldest }
2018-09-22 00:03:30 -04:00
DiscourseEvent.trigger(:donations_ready)
end