Class: Locomotive::Wagon::Generators::Section

Inherits:
Thor::Group
  • Object
show all
Includes:
CLI::ForceColor, Thor::Actions
Defined in:
lib/locomotive/wagon/generators/section.rb

Constant Summary collapse

ICON_LIST =
['header',
'default',
'slide',
'text',
'image_text',
'list',
'footer']

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CLI::ForceColor

#force_color_if_asked

Class Method Details

.source_rootObject



88
89
90
# File 'lib/locomotive/wagon/generators/section.rb', line 88

def self.source_root
  File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'generators', 'section')
end

Instance Method Details

#build_optionsObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/locomotive/wagon/generators/section.rb', line 39

def build_options
  @_slug   = slug.clone.downcase.gsub(/[-]/, '_')
  @options = {
    name:       @_slug.humanize,
    type:       @_slug,
    global:     @global,
    icon:       @icon,
    settings:   extract_section_settings,
    blocks:     extra_blocks,
    all_icons:  ICON_LIST
  }
end

#create_javascript_fileObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/locomotive/wagon/generators/section.rb', line 58

def create_javascript_file
  if File.exist?(sections_js_path)
    js_class_name = @options[:type].classify
    file_path     = File.join(sections_js_path, @options[:type])

    template "%type%.js.tt", "#{file_path}.js", @options

    append_to_file File.join(sections_js_path, 'index.js'), <<-JS
export { default as #{js_class_name} } from './#{@options[:type]}';
    JS

    insert_into_file 'app/assets/javascripts/app.js', after: "// Register sections here. DO NOT REMOVE OR UPDATE THIS LINE\n" do
      "  sectionsManager.registerSection('#{@options[:type]}', Sections.#{js_class_name});\n"
    end
  end
end

#create_sectionObject



52
53
54
55
56
# File 'lib/locomotive/wagon/generators/section.rb', line 52

def create_section
  # create the liquid file
  file_path = File.join(sections_path, @_slug)
  template "template.liquid.tt", "#{file_path}.liquid", @options
end

#create_stylesheet_fileObject



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/locomotive/wagon/generators/section.rb', line 75

def create_stylesheet_file
  if File.exist?(sections_css_path)
    css_class_name  = "#{@options[:type].dasherize}-section"
    file_path       = File.join(sections_css_path, @options[:type])

    template "%type%.scss.tt", "#{file_path}.scss", @options

    insert_into_file 'app/assets/stylesheets/app.scss', after: "// Register sections here. DO NOT REMOVE OR UPDATE THIS LINE\n" do
      "@import 'sections/#{@options[:type]}';\n"
    end
  end
end

#is_global?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
# File 'lib/locomotive/wagon/generators/section.rb', line 26

def is_global?
  if (@global = self.options[:global]).nil?
    @global = yes?('Is this section aimed to be used as global (same content for all the pages)?')
  end
end

#which_icon?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
# File 'lib/locomotive/wagon/generators/section.rb', line 32

def which_icon?
  if (@icon = self.options[:icon]).nil?
    question = 'Which icon should be displayed in the editor UI?'
    @icon = ask(question, limited_to: ICON_LIST)
  end
end