Sha256: 7d1ca8cfb9a29fd7839a207ce22351667c997805f102e2094a7ff4922b861386

Contents?: true

Size: 1.9 KB

Versions: 3

Compression:

Stored size: 1.9 KB

Contents

class PaidUp::Plan < ActiveRecord::Base
  has_many :plan_feature_settings, class_name: 'PaidUp::PlanFeatureSetting', foreign_key: 'plan_id', inverse_of: :plan
  accepts_nested_attributes_for :plan_feature_settings

  after_initialize :load_stripe_data

  attr_accessor :stripe_data

  validates_presence_of :title, :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

3 entries across 3 versions & 1 rubygems

Version Path
paid_up-0.6.5 app/models/paid_up/plan.rb
paid_up-0.6.4 app/models/paid_up/plan.rb
paid_up-0.6.3 app/models/paid_up/plan.rb