Class: SpaceFrame::Generators::SectionGenerator

Inherits:
NamedBase
  • Object
show all
Defined in:
lib/generators/section/section_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ SectionGenerator

Returns a new instance of SectionGenerator.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/generators/section/section_generator.rb', line 10

def initialize(*args)
  super(*args)
  @logger = Logger.new STDOUT

  if @name == @name.pluralize && !options[:force_plural]
    @logger.info "You selected a plural resource name. It's best to use the singular version. Override with --force-plural"
    @name = @name.singularize
  end

  @controller_name = plural_table_name
  @view_name = plural_table_name
  @model_name = singular_table_name
end

Instance Attribute Details

#controller_name ⇒ Object (readonly)

Returns the value of attribute controller_name.



6
7
8
# File 'lib/generators/section/section_generator.rb', line 6

def controller_name
  @controller_name
end

#model_name ⇒ Object (readonly)

Returns the value of attribute model_name.



6
7
8
# File 'lib/generators/section/section_generator.rb', line 6

def model_name
  @model_name
end

#view_name ⇒ Object (readonly)

Returns the value of attribute view_name.



6
7
8
# File 'lib/generators/section/section_generator.rb', line 6

def view_name
  @view_name
end

Instance Method Details

#add_root_route ⇒ Object



48
49
50
# File 'lib/generators/section/section_generator.rb', line 48

def add_root_route
  route(%Q{root :to => "#{@controller_name}#index"})
end

#generate_section ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/generators/section/section_generator.rb', line 24

def generate_section
  directory "app", "app/assets/javascripts/app"

  insert_into_file "app/assets/javascripts/app/main.coffee",
                   "    @#{@controller_name} = new App.#{@controller_name.camelize}.Section()",
                   :after =>"super\n"

  gsub_file "app/assets/javascripts/app/main.coffee", /\@sections = \[(.*)\]/ do |match|
    puts match.to_s
    match = /\@sections = \[(.*)\]/.match(match.to_s)
    puts "match: #{match[1]}"

    if match == "sections = []"
      "@sections = [@#{@controller_name}]"
    else
      sections = match[1].split(",")
      sections << "@#{@controller_name}" unless sections.include?("@#{@controller_name}")
      "@sections = [#{sections.join(',')}]"
    end
  end

  #process other templates
end