| 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
44
45
46
 | 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 :name => user_name,: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( :user_name, user.errors[:name]) if user.errors[:name]
      self.errors.add( :password, user.errors[:password]) if user.errors[:password]
      self.errors.add( :password_confirmation, user.errors[:password_confirmation]) if user.errors[:password_confirmation]
      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
 |