Class: Ekylibre::FirstRun::Folder

Inherits:
Object
  • Object
show all
Defined in:
lib/ekylibre/first_run/folder.rb

Constant Summary collapse

VERSION =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ Folder

Returns a new instance of Folder.


8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ekylibre/first_run/folder.rb', line 8

def initialize(path, options = {})
  @verbose = !options[:verbose].is_a?(FalseClass)
  @path = Pathname.new(path)
  manifest = YAML.load_file(@path.join('manifest.yml')).deep_symbolize_keys
  @version = manifest[:version]
  unless @version == VERSION
    raise "Incompatible first-run folder: #{@version.inspect}." \
         "Need v#{VERSION} first-run API."
  end
  @company = manifest[:company] || {}
  @imports = manifest[:imports] || {}
  @preferences = manifest[:preferences] || {}
  @defaults = manifest[:defaults] || {}
end

Instance Attribute Details

#defaultsObject (readonly)

Returns the value of attribute defaults


6
7
8
# File 'lib/ekylibre/first_run/folder.rb', line 6

def defaults
  @defaults
end

#importsObject (readonly)

Returns the value of attribute imports


6
7
8
# File 'lib/ekylibre/first_run/folder.rb', line 6

def imports
  @imports
end

#pathObject (readonly)

Returns the value of attribute path


6
7
8
# File 'lib/ekylibre/first_run/folder.rb', line 6

def path
  @path
end

#preferencesObject (readonly)

Returns the value of attribute preferences


6
7
8
# File 'lib/ekylibre/first_run/folder.rb', line 6

def preferences
  @preferences
end

#verboseObject (readonly)

Returns the value of attribute verbose


6
7
8
# File 'lib/ekylibre/first_run/folder.rb', line 6

def verbose
  @verbose
end

#versionObject (readonly)

Returns the value of attribute version


6
7
8
# File 'lib/ekylibre/first_run/folder.rb', line 6

def version
  @version
end

Instance Method Details

#imports_dirObject


23
24
25
# File 'lib/ekylibre/first_run/folder.rb', line 23

def imports_dir
  @path.join('imports')
end

#load_companyObject

Load company informations (entity) and its activities


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ekylibre/first_run/folder.rb', line 69

def load_company
  company = Entity.find_or_initialize_by(of_company: true, nature: :organization)
  company.last_name = @company[:name]
  company.save!
  # Create default phone number
  unless @company[:phone].blank?
    phone = company.phones.find_or_initialize_by(by_default: true)
    phone.coordinate = @company[:phone]
    phone.save!
  end
  # Create default mail address
  unless @company[:mail_line_4].blank? && @company[:mail_line_6].blank?
    mail = company.mails.find_or_initialize_by(by_default: true)
    mail.mail_line_4 = @company[:mail_line_4]
    mail.mail_line_6 = @company[:mail_line_6]
    mail.save!
  end
  load_users(@company[:users]) if @company[:users]
end

#load_defaultsObject

Load default data of models with default data


57
58
59
60
61
62
63
64
65
66
# File 'lib/ekylibre/first_run/folder.rb', line 57

def load_defaults
  [:sequences, :accounts, :document_templates, :taxes, :journals, :cashes,
   :sale_natures, :purchase_natures, :incoming_payment_modes,
   :outgoing_payment_modes, :product_nature_variants, :map_backgrounds].each do |dataset|
    next if @defaults[dataset].is_a?(FalseClass)
    puts "Load default #{dataset}..."
    model = dataset.to_s.classify.constantize
    model.load_defaults
  end
end

#load_importsObject

Load all given imports directly (to ensure given order)


90
91
92
93
94
95
96
# File 'lib/ekylibre/first_run/folder.rb', line 90

def load_imports
  @imports.each do |name, import|
    import[:nature] ||= name
    puts "Import #{import[:nature].to_s.yellow} from #{import[:file].to_s.blue}"
    Import.launch!(import[:nature], path.join('imports', import[:file]))
  end
end

#load_preferencesObject

Load global preferences of the instance


45
46
47
48
49
50
51
52
53
54
# File 'lib/ekylibre/first_run/folder.rb', line 45

def load_preferences
  @preferences[:map_measure_srs] ||= 'WGS84'
  @preferences.each do |key, value|
    if Preference.reference[key]
      Preference.set!(key, value)
    else
      raise "Unknown preference: #{key}"
    end
  end
end

#load_users(users = {}) ⇒ Object

Load (administrator) users


99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ekylibre/first_run/folder.rb', line 99

def load_users(users = {})
  users.each do |email, attributes|
    defaults = {
      email: email,
      first_name: 'John',
      last_name: 'Doe',
      password: '12345678',
      administrator: true
    }
    unless User.find_by(email: email)
      User.create!(defaults.merge(attributes))
    end
  end
end

#runObject


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ekylibre/first_run/folder.rb', line 27

def run
  ActiveRecord::Base.transaction do
    puts 'Set locale...'
    ::I18n.locale = @preferences[:language] || :eng
    puts 'Load preferences...'
    load_preferences
    puts 'Load defaults...'
    load_defaults
    puts 'Load company...'
    load_company
    puts 'Load imports...'
    load_imports
    puts 'Save state...'
    ::Preference.set!('first_run.executed', false, :boolean)
  end
end