module Marionette::ResourceHelpers

Resource helpers

Public Instance Methods

backbone_path() click to toggle source
# File lib/generators/marionette/resource_helpers.rb, line 8
def backbone_path
  "#{javascript_path}/backbone"
end
controller_attr(file, type, attrs) click to toggle source
# File lib/generators/marionette/resource_helpers.rb, line 37
def controller_attr(file, type, attrs)
  sentinel = /when '#{type.to_s}'$/
  in_root do
    inject_into_file "#{backbone_path}/app/controllers/#{file}.js.coffee", "          #{attrs}", { after: sentinel, verbose: false }
  end
end
default_action() click to toggle source
# File lib/generators/marionette/resource_helpers.rb, line 12
def default_action
  'index'
end
default_controller() click to toggle source
# File lib/generators/marionette/resource_helpers.rb, line 16
def default_controller
  'application'
end
default_route() click to toggle source
# File lib/generators/marionette/resource_helpers.rb, line 20
def default_route
  "#{default_controller}##{default_action}"
end
embed_file(source, indent = '') click to toggle source
# File lib/generators/marionette/resource_helpers.rb, line 24
def embed_file(source, indent = '')
  IO.read(File.join(self.class.source_root, source)).gsub(/^/, indent)
end
embed_template(source, indent = '') click to toggle source
# File lib/generators/marionette/resource_helpers.rb, line 28
def embed_template(source, indent = '')
  template = File.join(self.class.source_root, source)
  ERB.new(IO.read(template), nil, '-').result(binding).gsub(/^/, indent)
end
javascript_path() click to toggle source
# File lib/generators/marionette/resource_helpers.rb, line 4
def javascript_path
  'app/assets/javascripts'
end
marionette_route(routing_code, type, modul='All') click to toggle source
# File lib/generators/marionette/resource_helpers.rb, line 51
    def marionette_route(routing_code, type, modul='All')
      sentinel = /appRoutes\:\s*$/

      route = routing_code

      case type
      when :index
        route = "    \"#{routing_code}(/)\": \"#{routing_code}_index\""
      when :new
        route = "    \"#{routing_code}/new(/)\": \"#{routing_code}_new\""
      when :show
        route = "    \"#{routing_code}/:id(/)\": \"#{routing_code}_show\""
      when :edit
        route = "    \"#{routing_code}/:id/edit(/)\": \"#{routing_code}_edit\""
      end

      log :marionette_route, route

      in_root do
        inject_into_file "#{backbone_path}/routes.js.coffee", "\n  #{route}", { after: sentinel, verbose: false }
      end


      case type
      when :index
        api = %(
    #{routing_code}_index: ->
      new App.Controllers.#{modul}.#{routing_code.camelize}(action: 'index')
)
      when :new
        api = %(
    #{routing_code}_new: ->
      new App.Controllers.#{modul}.#{routing_code.camelize}(action: 'new')
)
      when :show
        api = %(
    #{routing_code}_show: (id) ->
      new App.Controllers.#{modul}.#{routing_code.camelize}(id: id, action: 'show')
)
      when :edit
        api = %(
    #{routing_code}_edit: (id) ->
      new App.Controllers.#{modul}.#{routing_code.camelize}(id: id, action: 'edit')
)
      end

      sentinel = /API \=\s*$/

      in_root do
        inject_into_file "#{backbone_path}/routes.js.coffee", "\n#{api}", { after: sentinel, verbose: false }
      end
    end
replace_underscore(string) click to toggle source
# File lib/generators/marionette/resource_helpers.rb, line 33
def replace_underscore(string)
  string[0] == '_' ? string[1..-1] : string
end
view_attr(file, attrs) click to toggle source
# File lib/generators/marionette/resource_helpers.rb, line 44
def view_attr(file, attrs)
  sentinel = /extends.*$/
  in_root do
    inject_into_file "#{backbone_path}/app/views/#{file}.js.coffee", "\n#{attrs}", { after: sentinel, verbose: false }
  end
end