Module: PaidUp::Mixins::PaidFor::InstanceMethods

Defined in:
lib/paid_up/mixins/paid_for.rb

Overview

Included by paid_for mixin

Instance Method Summary collapse

Instance Method Details

#enabled?Boolean

Returns:

  • (Boolean)


114
115
116
117
118
119
120
121
122
# File 'lib/paid_up/mixins/paid_for.rb', line 114

def enabled?
  if owners_enabled_count >= owners_records_count
    true
  else
    owners_records.order(id: :asc)
                  .limit(owners_enabled_count)
                  .include? self
  end
end

#ownersObject



65
66
67
68
69
70
71
72
73
74
# File 'lib/paid_up/mixins/paid_for.rb', line 65

def owners
  case self.class.feature.setting_type
  when 'table_rows'
    [user]
  when 'rolify_rows'
    User.with_role(:owner, self)
  else
    raise :value_is_not_a_valid_setting_type.l
  end
end

#owners_enabled_countObject

How many records can this user have?



86
87
88
89
90
91
92
# File 'lib/paid_up/mixins/paid_for.rb', line 86

def owners_enabled_count
  setting = 0
  owners.each do |subscriber|
    setting += subscriber.plan.feature_setting(self.class.table_name)
  end
  setting
end

#owners_recordsObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/paid_up/mixins/paid_for.rb', line 94

def owners_records
  scoped_records = self.class.paid_for_scope
  case self.class.feature.setting_type
    when 'table_rows'
      scoped_records.where(user_id: user_id)
    when 'rolify_rows'
      scoped_records
          .includes(roles: :users)
          .references(:roles, :users)
          .where('roles.name = ?', 'owner')
          .where('users.id IN (?)', owners.ids)
    else
      raise :value_is_not_a_valid_setting_type.l
  end
end

#owners_records_countObject



110
111
112
# File 'lib/paid_up/mixins/paid_for.rb', line 110

def owners_records_count
  owners_records.size
end

#save_with_owner(owner) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/paid_up/mixins/paid_for.rb', line 76

def save_with_owner(owner)
  if save
    owner.add_role(:owner, self)
    self
  else
    false
  end
end