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

#default_datasetsObject


123
124
125
126
127
# File 'lib/ekylibre/first_run/folder.rb', line 123

def default_datasets
  %i[sequences accounts document_templates taxes journals cashes
     sale_natures purchase_natures incoming_payment_modes
     outgoing_payment_modes product_nature_variants map_layers naming_format_land_parcels]
end

#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


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ekylibre/first_run/folder.rb', line 74

def load_company
  company = Entity.find_or_initialize_by(of_company: true, nature: :organization)
  company.last_name = @company[:name]
  company.born_at = @company[:born_at]
  company.siret_number = @company[:siret_number]
  company.save!
  # Create default phone number
  if @company[:phone].present?
    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


63
64
65
66
67
68
69
70
71
# File 'lib/ekylibre/first_run/folder.rb', line 63

def load_defaults
  default_datasets.each do |dataset|
    next if @defaults[dataset].is_a?(FalseClass)
    puts "Load default #{dataset}..."
    model = dataset.to_s.classify.constantize
    model.load_defaults
    @progress.increment!
  end
end

#load_importsObject

Load all given imports directly (to ensure given order)


97
98
99
100
101
102
103
104
# File 'lib/ekylibre/first_run/folder.rb', line 97

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]))
    @progress.increment!
  end
end

#load_preferencesObject

Load global preferences of the instance


50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ekylibre/first_run/folder.rb', line 50

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

#load_users(users = {}) ⇒ Object

Load (administrator) users


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ekylibre/first_run/folder.rb', line 107

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
    @progress.increment!
  end
end

#runObject


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

def run
  step = 0
  [@preferences, @imports, @company[:users], default_datasets].each { |d| step += d.try(:count) || 0 }
  step += 1 if @preferences[:map_measure_srs].nil?
  @progress = Progress.new('first_run', max: step)
  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', true, :boolean)
  end
  @progress.clear!
end