effective_learndash 0.1.3 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +16 -10
  3. data/app/assets/config/effective_learndash_manifest.js +0 -1
  4. data/app/controllers/admin/course_registrations_controller.rb +8 -0
  5. data/app/controllers/admin/learndash_courses_controller.rb +1 -1
  6. data/app/controllers/effective/course_registrations_controller.rb +13 -0
  7. data/app/controllers/effective/learndash_courses_controller.rb +32 -0
  8. data/app/datatables/admin/effective_course_registrations_datatable.rb +28 -0
  9. data/app/datatables/admin/effective_learndash_courses_datatable.rb +5 -1
  10. data/app/datatables/admin/effective_learndash_enrollments_datatable.rb +1 -2
  11. data/app/datatables/admin/effective_learndash_users_datatable.rb +1 -1
  12. data/app/datatables/effective_course_registrants_datatable.rb +24 -0
  13. data/app/datatables/effective_course_registrations_datatable.rb +35 -0
  14. data/app/datatables/effective_learndash_courses_datatable.rb +30 -0
  15. data/app/datatables/effective_learndash_enrollments_datatable.rb +7 -4
  16. data/app/models/concerns/effective_learndash_course_registration.rb +172 -0
  17. data/app/models/concerns/effective_learndash_owner.rb +1 -1
  18. data/app/models/effective/course_registrant.rb +53 -0
  19. data/app/models/effective/course_registration.rb +5 -0
  20. data/app/models/effective/learndash_api.rb +14 -5
  21. data/app/models/effective/learndash_course.rb +78 -3
  22. data/app/models/effective/learndash_enrollment.rb +7 -3
  23. data/app/models/effective/learndash_user.rb +5 -6
  24. data/app/views/admin/learndash_courses/_form.html.haml +25 -0
  25. data/app/views/admin/learndash_courses/_form_course_registration_content.html.haml +19 -0
  26. data/app/views/admin/learndash_courses/_form_learndash_course.html.haml +32 -0
  27. data/app/views/admin/learndash_courses/_learndash_course.html.haml +3 -3
  28. data/app/views/admin/learndash_owners/_form.html.haml +1 -1
  29. data/app/views/admin/learndash_users/_form.html.haml +1 -1
  30. data/app/views/admin/learndash_users/_learndash_user.html.haml +4 -4
  31. data/app/views/effective/course_registrations/_content.html.haml +10 -0
  32. data/app/views/effective/course_registrations/_course.html.haml +9 -0
  33. data/app/views/effective/course_registrations/_course_registration.html.haml +3 -0
  34. data/app/views/effective/course_registrations/_dashboard.html.haml +30 -0
  35. data/app/views/effective/course_registrations/_layout.html.haml +3 -0
  36. data/app/views/effective/course_registrations/_orders.html.haml +4 -0
  37. data/app/views/effective/course_registrations/_summary.html.haml +39 -0
  38. data/app/views/effective/course_registrations/billing.html.haml +15 -0
  39. data/app/views/effective/course_registrations/checkout.html.haml +6 -0
  40. data/app/views/effective/course_registrations/course.html.haml +20 -0
  41. data/app/views/effective/course_registrations/start.html.haml +25 -0
  42. data/app/views/effective/course_registrations/submitted.html.haml +15 -0
  43. data/app/views/effective/course_registrations/summary.html.haml +8 -0
  44. data/app/views/effective/learndash/_dashboard.html.haml +14 -16
  45. data/app/views/effective/learndash_courses/_learndash_course.html.haml +13 -0
  46. data/app/views/effective/learndash_courses/index.html.haml +5 -0
  47. data/app/views/effective/learndash_courses/show.html.haml +6 -0
  48. data/config/effective_learndash.rb +9 -0
  49. data/config/routes.rb +13 -3
  50. data/db/migrate/01_create_effective_learndash.rb.erb +98 -33
  51. data/lib/effective_learndash/engine.rb +1 -0
  52. data/lib/effective_learndash/version.rb +1 -1
  53. data/lib/effective_learndash.rb +6 -1
  54. data/lib/generators/effective_learndash/install_generator.rb +0 -1
  55. metadata +33 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 81bcba4c739ea9bac4b39489414f6cefbaff0530deb01af45e18960072057476
4
- data.tar.gz: 461b4243fa49f7d9cc4a5aba4f2be46d991648328c42a8d81f7cadb193d21031
3
+ metadata.gz: bf3a76614af4d9fba4a5d9e483ecfc78ec821c042643b0d59508b42f80aead20
4
+ data.tar.gz: 3e383233f17438db0bc4c134e8fb767183b3697b0036100ddf89e80961b42332
5
5
  SHA512:
6
- metadata.gz: bf9e1c610922fd5a50d9217ca3edd65861599a21e68d6ccfacbe1ffe47276e0612645b691c92955fac10dbf03c2b30c6a0cf4051cf7616edf5bde243673e7ad4
7
- data.tar.gz: 30913570908343e2eb323a4a73fd646a3751db4916073e857abd16069d26f156d233149fe381a0d53b79bc63a45c61ce93dc9985598638a6fe5a3e2a6a190e40
6
+ metadata.gz: 73041dc5fa8d6e130f865cd0ed5acf4c966ff1d7148168d6482f582f68c29d86fd8521c6d23aeb35227ca1f70810bd54b88408527767c3654b0998f8f6513d37
7
+ data.tar.gz: 58d39dc3138ff44d2be3152aba69734f7f83146714b1b79a44b084cdc05dcd865974e457b4f61c2c3dd2436e5a81b709db7ea3cde5b68b807a325bf28df7c58e
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # Effective Learndash
1
+ # Effective LearnDash
2
2
 
3
- Create Wordpress users and read Learndash course progress. This is an unoffocial integration that is not supported or affiliated with WordPress or Learndash.
3
+ Create Wordpress users and read LearnDash course progress. This is an unofficial integration that is not supported or affiliated with WordPress or LearnDash.
4
4
 
5
5
  ## Getting Started
6
6
 
@@ -74,6 +74,12 @@ The permissions you actually want to define are as follows (using CanCan):
74
74
  can(:show, Effective::LearndashUser) { |lduser| lduser.owner_id == user.id }
75
75
  can(:index, Effective::LearndashEnrollment)
76
76
 
77
+ can([:index, :show], Effective::LearndashCourse) { |course| !course.draft? }
78
+ can([:show, :index], Effective::CourseRegistrant) { |registrant| registrant.owner == user || registrant.owner.blank? }
79
+ can([:new, :create], EffectiveLearndash.CourseRegistration)
80
+ can([:show, :index], EffectiveLearndash.CourseRegistration) { |registration| registration.owner == user }
81
+ can([:update, :destroy], EffectiveLearndash.CourseRegistration) { |registration| registration.owner == user && !registration.was_submitted? }
82
+
77
83
  if user.admin?
78
84
  can :admin, :effective_learndash
79
85
 
@@ -85,29 +91,29 @@ if user.admin?
85
91
  end
86
92
  ```
87
93
 
88
- ## Configuring Learndash
94
+ ## Configuring LearnDash
89
95
 
90
- Your Wordpress should be configured ahead of time with the Learndash plugin.
96
+ Your WordPress should be configured ahead of time with the LearnDash plugin.
91
97
 
92
98
  Please generate an application password via:
93
99
 
94
100
  https://make.wordpress.org/core/2020/11/05/application-passwords-integration-guide/
95
101
 
96
- and fill in the username/password details in config/initializers/effective_leardash.rb
102
+ and fill in the username/password details in config/initializers/effective_learndash.rb
97
103
 
98
- ## Working with Learndash
104
+ ## Working with LearnDash
99
105
 
100
106
  Visit `/admin/learndash_courses` and Refresh the list of Courses.
101
107
 
102
- Create a New Learndash User will create a new Wordpress/Learndash account with a username/password according to the settings in the config file.
108
+ Create a New LearnDash User will create a new WordPress/LearnDash account with a username/password according to the settings in the config file.
103
109
 
104
110
  When you create a user, you only get access to the password once. So any existing users will have an unknown password.
105
111
 
106
- Create a new Learndash Enrollment to enroll a learndash user into a course. This will begin tracking their progress.
112
+ Create a new LearnDash Enrollment to enroll a LearnDash user into a course. This will begin tracking their progress.
107
113
 
108
- There are no webhooks or callbacks from Learndash, everything is a GET request that updates the local database.
114
+ There are no webhooks or callbacks from LearnDash, everything is a GET request that updates the local database.
109
115
 
110
- You can refresh an entire learndash user in one operation and it will sync the entire user at once, `user.learndash_user.refresh!`
116
+ You can refresh an entire LearnDash user in one operation and it will sync the entire user at once, `user.learndash_user.refresh!`
111
117
 
112
118
  ## License
113
119
 
@@ -1,3 +1,2 @@
1
1
  //= link_directory ../javascripts .js
2
2
  //= link_directory ../stylesheets .css
3
- //= link_directory ../images/effective_learndash
@@ -0,0 +1,8 @@
1
+ module Admin
2
+ class CourseRegistrationsController < ApplicationController
3
+ before_action(:authenticate_user!) if defined?(Devise)
4
+ before_action { EffectiveResources.authorize!(self, :admin, :effective_learndash) }
5
+
6
+ include Effective::CrudController
7
+ end
8
+ end
@@ -7,7 +7,7 @@ module Admin
7
7
 
8
8
  def refresh
9
9
  resource_scope.refresh!
10
- flash[:success] = "Successfully refreshed Courses from Learndash"
10
+ flash[:success] = "Successfully refreshed Courses from LearnDash"
11
11
  redirect_to effective_learndash.admin_learndash_courses_path
12
12
  end
13
13
 
@@ -0,0 +1,13 @@
1
+ module Effective
2
+ class CourseRegistrationsController < ApplicationController
3
+ before_action(:authenticate_user!) if defined?(Devise)
4
+
5
+ include Effective::WizardController
6
+
7
+ resource_scope -> {
8
+ learndash_course = Effective::LearndashCourse.find(params[:learndash_course_id])
9
+ EffectiveLearndash.CourseRegistration.deep.where(owner: current_user, learndash_course: learndash_course)
10
+ }
11
+
12
+ end
13
+ end
@@ -0,0 +1,32 @@
1
+ module Effective
2
+ class LearndashCoursesController < ApplicationController
3
+ include Effective::CrudController
4
+
5
+ resource_scope -> {
6
+ unpublished = EffectiveResources.authorized?(self, :admin, :effective_learndash)
7
+ Effective::LearndashCourse.learndash_courses(user: current_user, unpublished: unpublished)
8
+ }
9
+
10
+ def show
11
+ @learndash_course = resource_scope.find(params[:id])
12
+
13
+ if @learndash_course.respond_to?(:roles_permit?)
14
+ raise Effective::AccessDenied.new('Access Denied', :show, @learndash_course) unless @learndash_course.roles_permit?(current_user)
15
+ end
16
+
17
+ EffectiveResources.authorize!(self, :show, @learndash_course)
18
+
19
+ if EffectiveResources.authorized?(self, :admin, :effective_learndash)
20
+ flash.now[:warning] = [
21
+ 'Hi Admin!',
22
+ ('You are viewing a hidden course.' if @learndash_course.draft?),
23
+ 'Click here to',
24
+ ("<a href='#{effective_learndash.edit_admin_learndash_course_path(@learndash_course)}' class='alert-link'>edit learndash course settings</a>.")
25
+ ].compact.join(' ')
26
+ end
27
+
28
+ @page_title ||= @learndash_course.to_s
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,28 @@
1
+ module Admin
2
+ class EffectiveCourseRegistrationsDatatable < Effective::Datatable
3
+ datatable do
4
+ col :updated_at, visible: false
5
+ col :created_at, visible: false
6
+ col :id, visible: false
7
+
8
+ col :token, visible: false
9
+
10
+ col :created_at, label: 'Created', visible: false
11
+ col :updated_at, label: 'Updated', visible: false
12
+ col :submitted_at, label: 'Submitted', visible: false, as: :date
13
+
14
+ col :learndash_course
15
+ col :owner
16
+
17
+ col :course_registrants, search: :string, visible: false
18
+ col :orders, label: 'Order'
19
+
20
+ actions_col
21
+ end
22
+
23
+ collection do
24
+ scope = EffectiveLearndash.CourseRegistration.all.deep.done
25
+ end
26
+
27
+ end
28
+ end
@@ -4,7 +4,7 @@ module Admin
4
4
  order :title
5
5
 
6
6
  col :id, visible: false
7
- col :course_id, label: 'Learndash Id', visible: false
7
+ col :course_id, label: 'LearnDash Id', visible: false
8
8
 
9
9
  col :title
10
10
  col :status
@@ -15,6 +15,10 @@ module Admin
15
15
 
16
16
  col :learndash_users, visible: false
17
17
 
18
+ col :can_register
19
+ col :regular_price, as: :price
20
+ col :member_price, as: :price
21
+
18
22
  actions_col
19
23
  end
20
24
 
@@ -14,8 +14,7 @@ module Admin
14
14
  time_ago_in_words(enrollment.last_synced_at) + ' ago'
15
15
  end
16
16
 
17
- col :owner, visible: false
18
-
17
+ col :owner
19
18
  col :learndash_course
20
19
  col :learndash_user
21
20
 
@@ -2,7 +2,7 @@ module Admin
2
2
  class EffectiveLearndashUsersDatatable < Effective::Datatable
3
3
  datatable do
4
4
  col :id, visible: false
5
- col :user_id, label: 'Learndash Id', visible: false
5
+ col :user_id, label: 'LearnDash Id', visible: false
6
6
 
7
7
  col :last_refreshed, visible: true do |user|
8
8
  time_ago_in_words(user.last_synced_at) + ' ago'
@@ -0,0 +1,24 @@
1
+ # Used on the Course Registrations courses step
2
+
3
+ class EffectiveCourseRegistrantsDatatable < Effective::Datatable
4
+ datatable do
5
+ col :learndash_course
6
+ col :owner, label: 'User', action: false
7
+ col :price, as: :price
8
+ end
9
+
10
+ collection do
11
+ scope = Effective::CourseRegistrant.deep.all
12
+
13
+ if attributes[:learndash_course_id].present?
14
+ scope = scope.where(learndash_course_id: attributes[:learndash_course_id])
15
+ end
16
+
17
+ if attributes[:course_registration_id].present?
18
+ scope = scope.where(course_registration_id: attributes[:course_registration_id])
19
+ end
20
+
21
+ scope
22
+ end
23
+
24
+ end
@@ -0,0 +1,35 @@
1
+ # Dashboard Course Registrations
2
+ class EffectiveCourseRegistrationsDatatable < Effective::Datatable
3
+ datatable do
4
+ order :created_at
5
+
6
+ col :token, visible: false
7
+ col :created_at, visible: false
8
+
9
+ col :learndash_course, label: 'Title', search: :string do |registration|
10
+ registration.learndash_course.to_s
11
+ end
12
+
13
+ col(:submitted_at, label: 'Registered on') do |registration|
14
+ registration.submitted_at&.strftime('%F') || 'Incomplete'
15
+ end
16
+
17
+ col :owner, visible: false, search: :string
18
+ col :status, visible: false
19
+ col :orders, action: :show, visible: false, search: :string
20
+
21
+ actions_col(actions: []) do |registration|
22
+ if registration.draft?
23
+ dropdown_link_to('Continue', effective_learndash.learndash_course_course_registration_build_path(registration.learndash_course, registration, registration.next_step), 'data-turbolinks' => false)
24
+ dropdown_link_to('Delete', effective_learndash.learndash_course_course_registration_path(registration.learndash_course, registration), 'data-confirm': "Really delete #{registration}?", 'data-method': :delete)
25
+ else
26
+ dropdown_link_to('Show', effective_learndash.learndash_course_course_registration_path(registration.learndash_course, registration))
27
+ end
28
+ end
29
+ end
30
+
31
+ collection do
32
+ EffectiveLearndash.CourseRegistration.deep.where(owner: current_user)
33
+ end
34
+
35
+ end
@@ -0,0 +1,30 @@
1
+ # Dashboard Courses
2
+ class EffectiveLearndashCoursesDatatable < Effective::Datatable
3
+ filters do
4
+ # Registerable should be first here, so when displayed as a simple datatable on the dashboard they only see registerable courses
5
+ scope :registerable
6
+ scope :all
7
+ end
8
+
9
+ datatable do
10
+ order :title
11
+ col :id, visible: false
12
+
13
+ col :title do |learndash_course|
14
+ learndash_course.to_s
15
+ end
16
+
17
+ actions_col show: false do |learndash_course|
18
+ if current_user.learndash_enrollment(course: learndash_course).present?
19
+ 'Registered. Please access from the home dashboard or applicant wizard.'
20
+ elsif learndash_course.can_register?
21
+ dropdown_link_to('Register', effective_learndash.new_learndash_course_course_registration_path(learndash_course))
22
+ end
23
+ end
24
+ end
25
+
26
+ collection do
27
+ Effective::LearndashCourse.deep.learndash_courses(user: current_user)
28
+ end
29
+
30
+ end
@@ -1,8 +1,11 @@
1
1
  # Dashboard LearndashUsers
2
2
  class EffectiveLearndashEnrollmentsDatatable < Effective::Datatable
3
3
  datatable do
4
- col :learndash_course
5
- col :progress_status
4
+ col :learndash_course, label: 'Title' do |enrollment|
5
+ enrollment.learndash_course.to_s
6
+ end
7
+
8
+ col :progress_status, label: 'Status'
6
9
 
7
10
  col :last_step, visible: false
8
11
  col :steps_completed, visible: false
@@ -13,9 +16,9 @@ class EffectiveLearndashEnrollmentsDatatable < Effective::Datatable
13
16
 
14
17
  actions_col(show: false) do |enrollment|
15
18
  if enrollment.not_started?
16
- dropdown_link_to('Start', EffectiveLearndash.learndash_url, target: '_blank')
19
+ dropdown_link_to('Access Course', EffectiveLearndash.learndash_url, target: '_blank')
17
20
  elsif enrollment.in_progress?
18
- dropdown_link_to('Continue', EffectiveLearndash.learndash_url, target: '_blank')
21
+ dropdown_link_to('Continue Course', EffectiveLearndash.learndash_url, target: '_blank')
19
22
  else
20
23
  dropdown_link_to('Show', EffectiveLearndash.learndash_url, target: '_blank')
21
24
  end
@@ -0,0 +1,172 @@
1
+ # frozen_stcourse_registrant_literal: true
2
+
3
+ # EffectiveLearndashCourseRegistration
4
+ #
5
+ # Mark your owner model with effective_learndash_course_registration to get all the includes
6
+
7
+ module EffectiveLearndashCourseRegistration
8
+ extend ActiveSupport::Concern
9
+
10
+ module Base
11
+ def effective_learndash_course_registration
12
+ include ::EffectiveLearndashCourseRegistration
13
+ end
14
+ end
15
+
16
+ module ClassMethods
17
+ def effective_learndash_course_registration?; true; end
18
+ end
19
+
20
+ included do
21
+ acts_as_purchasable_parent
22
+ acts_as_tokened
23
+
24
+ acts_as_statused(
25
+ :draft, # Just Started
26
+ :submitted # All done
27
+ )
28
+
29
+ acts_as_wizard(
30
+ start: 'Start',
31
+ course: 'Course',
32
+ summary: 'Review',
33
+ billing: 'Billing Address',
34
+ checkout: 'Checkout',
35
+ submitted: 'Submitted'
36
+ )
37
+
38
+ acts_as_purchasable_wizard
39
+
40
+ log_changes(except: :wizard_steps) if respond_to?(:log_changes)
41
+
42
+ # Application Namespace
43
+ belongs_to :owner, polymorphic: true
44
+ accepts_nested_attributes_for :owner
45
+
46
+ # Effective Namespace
47
+ belongs_to :learndash_course, class_name: 'Effective::LearndashCourse'
48
+
49
+ has_many :course_registrants, -> { order(:id) }, class_name: 'Effective::CourseRegistrant', inverse_of: :course_registration, dependent: :destroy
50
+ accepts_nested_attributes_for :course_registrants, reject_if: :all_blank, allow_destroy: true
51
+
52
+ has_many :orders, -> { order(:id) }, as: :parent, class_name: 'Effective::Order', dependent: :nullify
53
+ accepts_nested_attributes_for :orders
54
+
55
+ effective_resource do
56
+ # Acts as Statused
57
+ status :string, permitted: false
58
+ status_steps :text, permitted: false
59
+
60
+ # Dates
61
+ submitted_at :datetime
62
+
63
+ # Acts as Wizard
64
+ wizard_steps :text, permitted: false
65
+
66
+ timestamps
67
+ end
68
+
69
+ scope :deep, -> { includes(:owner, :orders, :course_registrants) }
70
+ scope :sorted, -> { order(:id) }
71
+
72
+ scope :in_progress, -> { where.not(status: [:submitted]) }
73
+ scope :done, -> { where(status: [:submitted]) }
74
+
75
+ scope :for, -> (user) { where(owner: user) }
76
+
77
+ # All Steps validations
78
+ validates :owner, presence: true
79
+
80
+ # Course Step
81
+ validate(if: -> { current_step == :course }) do
82
+ self.errors.add(:course_registrants, "can't be blank") unless present_course_registrants.present?
83
+ end
84
+
85
+ # All Fees and Orders
86
+ def submit_fees
87
+ course_registrants
88
+ end
89
+
90
+ # Enroll them in all courses.
91
+ def after_submit_purchased!
92
+ course_registrants.each { |registrant| registrant.owner.create_learndash_enrollment(course: registrant.learndash_course) }
93
+ end
94
+
95
+ end
96
+
97
+ # Instance Methods
98
+ def to_s
99
+ persisted? ? "#{learndash_course} - #{owner}" : 'course registration'
100
+ end
101
+
102
+ def course
103
+ learndash_course
104
+ end
105
+
106
+ def in_progress?
107
+ draft?
108
+ end
109
+
110
+ def done?
111
+ submitted?
112
+ end
113
+
114
+ def course_registrant
115
+ course_registrants.first
116
+ end
117
+
118
+ def build_course_registrant
119
+ course_registrants.build(owner: owner, learndash_course: learndash_course)
120
+ end
121
+
122
+ def learndash_owner
123
+ (course_registrant&.owner || owner)
124
+ end
125
+
126
+ def member_pricing?
127
+ learndash_owner.membership_present?
128
+ end
129
+
130
+ def registration_price
131
+ raise('expected a learndash course') unless learndash_course.present?
132
+ raise('expected a learndash owner') unless learndash_owner.present?
133
+
134
+ member_pricing? ? learndash_course.member_price : learndash_course.regular_price
135
+ end
136
+
137
+ def assign_pricing
138
+ course_registrant.assign_attributes(
139
+ price: registration_price,
140
+ qb_item_name: learndash_course.qb_item_name,
141
+ tax_exempt: learndash_course.tax_exempt
142
+ )
143
+ end
144
+
145
+ # After the course step.
146
+ def course!
147
+ raise('expected a learndash course') unless learndash_course.present?
148
+ raise('expected a course_registrant to be present') unless course_registrant.present?
149
+ raise('expected a course_registrant to have an owner') unless course_registrant.owner.present?
150
+ raise('expected a learndash owner') unless learndash_owner.class.try(:effective_learndash_owner?)
151
+
152
+ # Assign prices
153
+ assign_pricing()
154
+ raise('expected course_registrants to have a price') if course_registrants.any? { |registrant| registrant.price.blank? }
155
+
156
+ # Save record
157
+ save!
158
+
159
+ # Create a learndash user now before payment to catch any errors in server stuff before payment.
160
+ learndash_owner.create_learndash_user
161
+ raise('expected a persisted learndash user') unless learndash_owner.learndash_user&.persisted?
162
+
163
+ true
164
+ end
165
+
166
+ private
167
+
168
+ def present_course_registrants
169
+ course_registrants.reject(&:marked_for_destruction?)
170
+ end
171
+
172
+ end
@@ -57,7 +57,7 @@ module EffectiveLearndashOwner
57
57
  return true if enrollment.completed?
58
58
 
59
59
  # Check the API
60
- enrollment.sync!
60
+ enrollment.refresh!
61
61
  enrollment.completed?
62
62
  end
63
63
 
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This is the registrant as created by the course registration
4
+ # It tracks who purchased which course for how much
5
+
6
+ module Effective
7
+ class CourseRegistrant < ActiveRecord::Base
8
+ acts_as_purchasable
9
+
10
+ log_changes(to: :learndash_course) if respond_to?(:log_changes)
11
+
12
+ # The learndash course containing all the pricing and unique info about this course purchase
13
+ belongs_to :learndash_course
14
+
15
+ # Every course is charged to a owner
16
+ belongs_to :owner, polymorphic: true
17
+
18
+ # As checked out through the learndash course purchase wizard
19
+ belongs_to :course_registration, polymorphic: true
20
+
21
+ effective_resource do
22
+ # Acts as Purchasable
23
+ price :integer
24
+ qb_item_name :string
25
+ tax_exempt :boolean
26
+
27
+ timestamps
28
+ end
29
+
30
+ scope :sorted, -> { order(:id) }
31
+ scope :deep, -> { includes(:learndash_course, :owner, :course_registration) }
32
+
33
+ before_validation(if: -> { course_registration.present? }) do
34
+ self.learndash_course ||= course_registration.learndash_course
35
+ self.owner ||= course_registration.owner
36
+ end
37
+
38
+ before_validation(if: -> { learndash_course.present? }) do
39
+ self.price ||= learndash_course.price
40
+ self.qb_item_name ||= learndash_course.qb_item_name
41
+ self.tax_exempt = learndash_course.tax_exempt? if self.tax_exempt.nil?
42
+ end
43
+
44
+ def to_s
45
+ persisted? ? title : 'course purchase'
46
+ end
47
+
48
+ def title
49
+ "#{learndash_course} - #{owner}"
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,5 @@
1
+ module Effective
2
+ class CourseRegistration < ActiveRecord::Base
3
+ effective_learndash_course_registration
4
+ end
5
+ end
@@ -60,13 +60,14 @@ module Effective
60
60
 
61
61
  username = username_for(owner)
62
62
  password = password_for(owner)
63
+ email = email_for(owner)
63
64
 
64
65
  payload = {
65
66
  username: username,
66
67
  password: password,
68
+ email: email,
67
69
 
68
70
  name: owner.to_s,
69
- email: owner.email,
70
71
  roles: ['subscriber'],
71
72
 
72
73
  first_name: owner.try(:first_name),
@@ -145,15 +146,23 @@ module Effective
145
146
  end
146
147
 
147
148
  def username_for(resource)
148
- raise('expected a learndash owner') unless resource.class.respond_to?(:effective_learndash_owner?) # This is a user
149
+ raise('expected a LearnDash owner') unless resource.class.respond_to?(:effective_learndash_owner?) # This is a user
149
150
 
150
151
  name = EffectiveLearndash.wp_username_for(resource)
151
152
  name = "test#{name}" unless Rails.env.production?
152
153
  name
153
154
  end
154
155
 
156
+ def email_for(resource)
157
+ raise('expected a LearnDash owner') unless resource.class.respond_to?(:effective_learndash_owner?) # This is a user
158
+
159
+ email = resource.email
160
+ email = "test#{email}" unless Rails.env.production?
161
+ email
162
+ end
163
+
155
164
  def password_for(resource)
156
- raise('expected a learndash owner') unless resource.class.respond_to?(:effective_learndash_owner?) # This is a user
165
+ raise('expected a LearnDash owner') unless resource.class.respond_to?(:effective_learndash_owner?) # This is a user
157
166
  EffectiveLearndash.wp_password_for(resource)
158
167
  end
159
168
 
@@ -169,7 +178,7 @@ module Effective
169
178
  elsif response.kind_of?(Array)
170
179
  response.first
171
180
  else
172
- raise("unexpected Learndash API response #{response}")
181
+ raise("unexpected LearnDash API response #{response}")
173
182
  end
174
183
  end
175
184
 
@@ -190,7 +199,7 @@ module Effective
190
199
  elsif response.kind_of?(Array)
191
200
  response
192
201
  else
193
- raise("unexpected Learndash API find_by response #{response}")
202
+ raise("unexpected LearnDash API find_by response #{response}")
194
203
  end
195
204
  )
196
205