Permalink
Browse files

Refactoring mixins way of defining methods

  • Loading branch information...
chadlundgren committed Mar 9, 2017
1 parent b2fedf4 commit a6105cca6e183ce6f94a4202effef974acfc50f3
Showing with 253 additions and 225 deletions.
  1. +2 −1 .gitignore
  2. +2 −0 Gemfile
  3. +2 −2 lib/paid_up.rb
  4. +79 −74 lib/paid_up/mixins/paid_for.rb
  5. +168 −148 lib/paid_up/mixins/subscriber.rb
@@ -6,4 +6,5 @@ spec/dummy/tmp
spec/dummy/log/*.log
coverage
.idea
Gemfile.lock
Gemfile.lock
.env*
@@ -1,5 +1,7 @@
source 'https://rubygems.org'
gem 'dotenv-rails', '~> 2.2', require: 'dotenv/rails-now', groups: [:development, :test]
gem 'rails', '~> 4.2'
gem 'rails-i18n', '~> 4'
gem 'haml-rails', '~> 0.9'
@@ -40,5 +40,5 @@ module PaidUp
Stripe::APIResource.send(:include, PaidUp::Extensions::Stripe)
ActiveRecord::Base.send(:include, PaidUp::Mixins::Subscriber)
ActiveRecord::Base.send(:include, PaidUp::Mixins::PaidFor)
ActiveRecord::Base.send(:extend, PaidUp::Mixins::Subscriber)
ActiveRecord::Base.send(:extend, PaidUp::Mixins::PaidFor)
@@ -4,102 +4,107 @@ module Mixins
module PaidFor
extend ActiveSupport::Concern
class_methods do
attr_accessor :paid_for_scope_symbol # Creates class-level instance var
def paid_for(options = {})
cattr_accessor :paid_for_scope_symbol # Creates class-level instance var
send :extend, ClassMethods
send :include, InstanceMethods
self.paid_for_scope_symbol = options.fetch(:scope, :all)
feature.nil? && raise(
:feature_not_found_feature.l(feature: table_name)
)
case feature.setting_type
when 'boolean'
# Nothing needs doing
when 'rolify_rows'
resourcify
attr_accessor :owner
when 'table_rows'
belongs_to :user
User.has_many table_name.to_sym
else
raise(
:value_is_not_a_valid_setting_type.l(
value: feature.setting_type
)
)
end
end
module ClassMethods
def feature
PaidUp::Feature.find_by_slug(table_name)
end
def paid_for_scope
send(self.paid_for_scope_symbol)
end
end
def paid_for(options = {})
self.paid_for_scope_symbol = options.fetch(:scope, :all)
feature.nil? && raise(
:feature_not_found_feature.l(feature: table_name)
)
case feature.setting_type
when 'boolean'
# Nothing needs doing
when 'rolify_rows'
resourcify
attr_accessor :owner
module InstanceMethods
def owners
case self.class.feature.setting_type
when 'table_rows'
belongs_to :user
User.has_many table_name.to_sym
[user]
when 'rolify_rows'
User.with_role(:owner, self)
end
end
def save_with_owner(owner)
if save
owner.add_role(:owner, self)
self
else
raise(
:value_is_not_a_valid_setting_type.l(
value: feature.setting_type
)
)
false
end
end
send(:define_method, :owners) do
# How many records can this user have?
def owners_enabled_count
setting = 0
owners.each do |subscriber|
setting += subscriber.plan.feature_setting(self.class.table_name)
end
setting
end
def owners_records
ids = []
owners.each do |subscriber|
case self.class.feature.setting_type
when 'table_rows'
[user]
ids += subscriber.send(self.class.table_name)
.paid_for_scope
.ids
when 'rolify_rows'
User.with_role(:owner, self)
end
end
send(:define_method, :save_with_owner) do |owner|
if save
owner.add_role(:owner, self)
self
ids += self.class
.with_role(:owner, subscriber)
.paid_for_scope
.ids
else
false
end
end
# How many records can this user have?
send(:define_method, :owners_enabled_count) do
setting = 0
owners.each do |subscriber|
setting += subscriber.plan.feature_setting(self.class.table_name)
end
setting
end
send(:define_method, :owners_records) do
ids = []
owners.each do |subscriber|
case self.class.feature.setting_type
when 'table_rows'
ids += subscriber.send(self.class.table_name)
.paid_for_scope
.ids
when 'rolify_rows'
ids += self.class
.with_role(:owner, subscriber)
.paid_for_scope
.ids
else
raise(
:no_features_associated_with_table.l(
table: self.class.table_name
)
raise(
:no_features_associated_with_table.l(
table: self.class.table_name
)
end
)
end
self.class.where(id: ids)
end
self.class.where(id: ids)
end
send(:define_method, :owners_records_count) do
owners_records.count
end
def owners_records_count
owners_records.count
end
send(:define_method, :enabled) do
if owners_enabled_count >= owners_records_count
true
else
enabled_records = owners_records.order('created_at ASC')
.limit(owners_enabled_count)
enabled_records.include? self
end
def enabled
if owners_enabled_count >= owners_records_count
true
else
enabled_records = owners_records.order('created_at ASC')
.limit(owners_enabled_count)
enabled_records.include? self
end
end
end
Oops, something went wrong.

0 comments on commit a6105cc

Please sign in to comment.