Created
November 12, 2012 13:12
-
-
Save pnegri/4059321 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Assinatura < ActiveResource::Base | |
| #self.site = "http://localhost:3001" | |
| self.site = "http://assinaturas.iugu.com.br" | |
| def self.api_key | |
| 'smallandsmart' | |
| end | |
| def self.find(*args) | |
| options = args.last.is_a?(Hash) ? args.pop : {} | |
| #if not options.include? :params | |
| options[:params] = Hash.new if options[:params].nil? | |
| #end | |
| options[:params][:api] = self.api_key | |
| args.push(options) | |
| super | |
| end | |
| def save | |
| prefix_options[:api] = Assinatura.api_key | |
| #self.attributes.delete :id | |
| super | |
| end | |
| def destroy | |
| prefix_options['api'] = Assinatura.api_key | |
| super | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # encoding: utf-8 | |
| class Conta < ActiveRecord::Base | |
| # include ActionView::Helpers::NumberHelper | |
| validates_presence_of :nome | |
| validates_inclusion_of :plano_id, :in => PLANOS.keys | |
| validates_presence_of :time_zone | |
| validates_presence_of :assinatura_id | |
| validates_numericality_of :assinatura_id, :only_integer => true, :allow_nil => false, :greater_than => 0, :message => 'não foi possivel' | |
| before_create :calcular_expiracao | |
| before_validation :atribuir_id_assinatura | |
| after_save :atualizar_dados_remotos | |
| after_destroy :excluir_assinatura | |
| after_rollback :excluir_assinatura | |
| has_many :hosts, :class_name => 'Host', :dependent => :destroy | |
| has_many :usuarios, :class_name => 'Usuario', :dependent => :destroy | |
| accepts_nested_attributes_for :hosts | |
| accepts_nested_attributes_for :usuarios | |
| def plano | |
| PLANOS[self.plano_id] unless PLANOS[self.plano_id].nil? | |
| end | |
| def plano_descricao | |
| self.plano['descricao'] unless self.plano.nil? | |
| end | |
| def plano_valor | |
| (self.plano['valor'] / 100.0) unless self.plano.nil? | |
| end | |
| private | |
| def excluir_assinatura | |
| unless self.assinatura_id.nil? | |
| assinatura = Assinatura.find( self.assinatura_id ) | |
| unless assinatura.nil? | |
| begin | |
| assinatura.destroy | |
| rescue | |
| true | |
| else | |
| true | |
| end | |
| end | |
| end | |
| self.assinatura_id = nil | |
| true | |
| end | |
| def calcular_expiracao | |
| #while data_final.wday == 0 || data_final.wday == 6 do | |
| # data_final = data_final.tomorrow | |
| #end | |
| self.expiracao = Date.today + IUGU_CONFIG['periodo_teste'].days unless IUGU_CONFIG['periodo_teste'].nil? | |
| end | |
| def atualizar_dados_remotos | |
| unless self.assinatura_id.nil? | |
| assinatura = Assinatura.find( self.assinatura_id ) | |
| unless assinatura.nil? | |
| assinatura.titulo = self.nome | |
| assinatura.nome = self.usuarios.first.nome | |
| assinatura.email = self.usuarios.first.email | |
| assinatura.plano = self.plano_id | |
| assinatura.valor = self.plano['valor'] | |
| assinatura.moeda = 'BRL' | |
| assinatura.url_servico = self.hosts.first.calcular_endereco | |
| assinatura.save! | |
| end | |
| end | |
| end | |
| def atribuir_id_assinatura | |
| #self.assinatura_id = 0 | |
| if self.assinatura_id == 0 || self.assinatura_id.nil? | |
| expiracao = self.expiracao | |
| if expiracao.nil? | |
| expiracao = Date.today + IUGU_CONFIG['periodo_teste'].days unless IUGU_CONFIG['periodo_teste'].nil? | |
| end | |
| begin | |
| assinatura = Assinatura.create( | |
| :nome => 'Nova assinatura', | |
| :email => 'nobody@iugu.com.br', | |
| :titulo => 'conta', | |
| :aplicativo => IUGU_CONFIG['app_name'], | |
| :plano => self.plano_id, | |
| :valor => self.plano['valor'], | |
| :moeda => 'BRL', | |
| :expiracao => expiracao, | |
| :url_servico => 'n/a' | |
| ) | |
| self.assinatura_id = assinatura.id unless assinatura.id.nil? | |
| rescue | |
| assinatura = nil | |
| self.assinatura_id = nil | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment