aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/subscription.rb
blob: 8b2b025deafb4f45e5c6f09ba0bf5691c3931612 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Subscription
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming
 
  attr_accessor :organisation_name, :user_name, :email, :password, :password_confirmation
 
  def initialize(attributes = {})  
    attributes.each do |name, value|  
      send("#{name}=", value)  
    end  
  end

  def persisted?  
    false  
  end  

  def user
    @user ||= organisation.users.build :email => email, :password => password, :password_confirmation => password_confirmation
  end

  def organisation
    @organisation ||= Organisation.new :name => organisation_name
  end

  def valid?
    unless organisation.valid?
      self.errors.add( :organisation_name, organisation.errors[:name]) if organisation.errors[:name]
    end
    unless user.valid?
      self.errors.add( :password, user.errors[:password]) if user.errors[:password]
      self.errors.add( :email, user.errors[:email]) if user.errors[:email]
    end
    self.errors.empty?
  end

  def save
    if valid?
      organisation.save and user.save
    end
  end

end