Sha256: 64d11114845345000fe5674991dd7c5d2f579475200b9effc5c2b80c175a0f20

Contents?: true

Size: 1.98 KB

Versions: 8

Compression:

Stored size: 1.98 KB

Contents

class PaidUp::Plan < ActiveRecord::Base
  has_many :plan_feature_settings, class_name: 'PaidUp::PlanFeatureSetting'
  has_many :features, :through => :plan_feature_settings, class_name: 'PaidUp::Feature'
  has_many :subscribers, :through => :subscriptions, :source => :subscriber, :source_type => 'User'

  after_initialize :load_stripe_data

  attr_accessor :stripe_data

  validates_presence_of :name, :stripe_id

  default_scope { order('sort_order ASC') }
  scope :subscribable, -> { where('sort_order >=  ?', 0) }
  scope :free, -> { find_by_stripe_id(PaidUp.configuration.free_plan_stripe_id) }

  def reload(*args, &blk)
    super *args, &blk
    load_stripe_data
    self
  end

  def feature_setting(feature_name)
    feature = PaidUp::Feature.find_by_slug(feature_name) || raise(:feature_not_found.l)
    raw = plan_feature_settings.where(feature: feature_name)
    case feature.setting_type
      when 'boolean'
        if raw.empty?
          false
        else
          raw.first.setting != 0
        end
      when 'table_rows', 'rolify_rows'
        if raw.empty?
          0
        else
          raw.first.setting
        end
      else
        raise :error_handling_feature_setting.l feature: feature
    end
  end

  def feature_unlimited?(feature_name)
    feature_setting(feature_name) == PaidUp::Unlimited.to_i
  end

  def interval
    if stripe_data.present?
      stripe_data.interval
    else
      :default_interval.l
    end
  end

  def interval_count
    if stripe_data.present?
      stripe_data.interval_count
    else
      1
    end
  end

  def amount
    if stripe_data.present?
      stripe_data.amount
    else
      0
    end
  end

  def money
    Money.new(amount, currency)
  end

  def charge
    money.amount
  end

  def currency
    if stripe_data.present?
      stripe_data.currency.upcase
    else
      :default_currency.l.upcase
    end
  end

  private

  def load_stripe_data
    if stripe_id.present?
      self.stripe_data = Stripe::Plan.retrieve stripe_id
    end
  end

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
paid_up-0.5.2 app/models/paid_up/plan.rb
paid_up-0.5.1 app/models/paid_up/plan.rb
paid_up-0.5.0 app/models/paid_up/plan.rb
paid_up-0.4.4 app/models/paid_up/plan.rb
paid_up-0.4.3 app/models/paid_up/plan.rb
paid_up-0.4.2 app/models/paid_up/plan.rb
paid_up-0.4.1 app/models/paid_up/plan.rb
paid_up-0.4.0 app/models/paid_up/plan.rb