Class: Binda::SetupGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/binda/setup/setup_generator.rb

Overview

Setup initial settings for the application.

This is setup is mandatory as sets the initial super admin user and

the default dashboard where are stored the main application settings.
It is useful also when Binda has been already installed once but the
database has been reset. Runnin `rails g binda:setup` will populate 
the application database with new default settings.

Instance Method Summary collapse

Instance Method Details

#create_credentialsObject



25
26
27
28
29
# File 'lib/generators/binda/setup/setup_generator.rb', line 25

def create_credentials
  puts "1) Create a superadmin user"
  rake 'binda:create_superadmin_user'
  puts 
end

#feedbackObject



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/generators/binda/setup/setup_generator.rb', line 89

def feedback
  puts "==============================================================================="
  puts
  puts "                 Binda CMS has been succesfully installed! "
  puts
  puts "==============================================================================="
  puts
  puts "Before deploying to production, remember to uncomment and update the"
  puts "'config.action_mailer.default_url_options' in 'config/environments/production.rb'"
  puts
  puts "==============================================================================="
end

#setup_default_helpersObject

Setup default helpers

This operation creates a class called B from which is possible to call any

Binda helper contained in Binda::DefaultHelpers. This is possible by inheriting the
`Binda::B` class.


82
83
84
85
86
87
# File 'lib/generators/binda/setup/setup_generator.rb', line 82

def setup_default_helpers
  puts "5) Setting up default helpers"
  generate "model", "B --no-migration --parent=::Binda::B"
  puts "Default helpers has been set up."
  puts 
end

#setup_maintenance_modeObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/generators/binda/setup/setup_generator.rb', line 31

def setup_maintenance_mode
  puts "2) Setting up maintenance mode"

  # Use radio field_type untill truefalse isn't available
  unless @field_settings.find_by(slug: 'maintenance-mode').present?
    maintenance_mode = @field_settings.create!( name: 'Maintenance Mode', field_type: 'radio', position: 1 )
    # create active and disabled choices
    maintenance_mode.choices.create!( label: 'active', value: 'true' )
    maintenance_mode.choices.create!( label: 'disabled', value: 'false' )
    radio = @dashboard.radios.find_or_create_by!( field_setting_id: maintenance_mode.id )
    radio.choices << maintenance_mode.choices.last
    radio.save!
    # make sure slug works
    maintenance_mode.update_attributes( slug: 'maintenance-mode' )
  end
  puts "The maintenance-mode option has been set up."
  puts 
end

#setup_settingsObject



14
15
16
17
18
19
20
21
22
23
# File 'lib/generators/binda/setup/setup_generator.rb', line 14

def setup_settings
  puts "Implement Binda settings"
  puts 

  dashboard_structure = ::Binda::Structure.find_or_create_by( name: 'dashboard', slug: 'dashboard', instance_type: 'board' )
  @dashboard = dashboard_structure.board

  # By default each structure has a field group which will be used to store the default field settings
  @field_settings = dashboard_structure.field_groups.first.field_settings
end

#setup_website_contentObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/generators/binda/setup/setup_generator.rb', line 64

def setup_website_content
  puts "4) Setting up website description"

  website_description_obj = @field_settings.find_by(slug: 'website-description')
  unless website_description_obj.present?
    website_description_obj = @field_settings.find_or_create_by( name: 'Website Description', field_type: 'text', position: 3 )
    # make sure slug works
    website_description_obj.update_attribute( 'slug', 'website-description' )
  end
  website_description = ask("What is your website about? ['A website about the world']\n").presence || 'A website about the world'
  @dashboard.texts.find_or_create_by!( field_setting_id: website_description_obj.id ).update_attribute( 'content', website_description )
end

#setup_website_nameObject



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/generators/binda/setup/setup_generator.rb', line 50

def setup_website_name 
  puts "3) Setting up website name"
  puts "We need few details. Don't worry you can modify them later."

  website_name_obj = @field_settings.find_by(slug: 'website-name')
  unless website_name_obj.present?
    website_name_obj = @field_settings.create!( name: 'Website Name', field_type: 'string', position: 2 )
    # make sure slug works
    website_name_obj.update_attribute( 'slug', 'website-name' )
  end
  website_name = ask("How would you like to name your website? ['MySite']\n").presence || 'MySite'
  @dashboard.strings.find_or_create_by( field_setting_id: website_name_obj.id ).update_attribute('content', website_name )
end