Module: Generators::Hobo

Defined in:
lib/generators/hobo/plugin.rb,
lib/generators/hobo/taglib.rb,
lib/generators/hobo/subsite.rb,
lib/generators/hobo/controller.rb,
lib/generators/hobo/invite_only.rb,
lib/generators/hobo/routes/router.rb,
lib/generators/hobo/activation_email.rb

Defined Under Namespace

Modules: Routes

Constant Summary collapse

Plugin =
classy_module do

  protected
  def gem_with_comments(*args)
    options = args.extract_options!
    name = args[0]

    unless File.read("Gemfile") =~ /^gem ("|')#{name}/
      if (comments = options.delete(:comments))
        append_file "Gemfile", "#{comments}", :verbose => false
      end

      gem(args[0], args[1], options)
      true
    else
      false
    end
  end

  def install_plugin_helper(name, git_path, options)
    plugin = name.dup
    unless options[:skip_gem]
      gem_options = {}
      if git_path
        if git_path =~ /:/
          gem_options[:git] = git_path
        else
          gem_options[:path] = git_path
        end
      end
      gem_options[:comments] = "# #{options[:comments]}" if options[:comments]
      need_update = gem_with_comments(plugin, options[:version], gem_options)
    end

    if options[:subsite].nil? || options[:subsite] == "all"
      subsites = ['front'] + ::Hobo.subsites
    else
      subsites = [options[:subsite]]
    end

    subsites.each do |subsite|
      inject_js_require(name, subsite, options[:comments]) unless options[:skip_js]
      inject_css_require(name, subsite, options[:comments]) unless options[:skip_css]
      inject_dryml_include(name, subsite, options[:comments]) unless options[:skip_dryml]
    end

    return need_update
  end

  def inject_js_require(name, subsite, comments)
    application_file = "app/assets/javascripts/#{subsite}.js"
    pattern          = /\/\/=(?!.*\/\/=).*?$/m

    unless exists?(application_file)
      application_file = "#{application_file}.coffee"
      pattern          = /#=(?!.*#=).*?$/m
    end

    raise Thor::Error, "Couldn't find either #{subsite}.js or #{subsite}.js.coffee files!" unless exists?(application_file)

    inject_into_file application_file, :before=>pattern do
      line = "//= require #{name}\n"
      line = "//\n// #{comments}\n#{line}" if comments
      line
    end
  end

  def inject_css_require(name, subsite, comments)
    application_file = "app/assets/stylesheets/#{subsite}.scss"
    application_file = "app/assets/stylesheets/#{subsite}.css" unless exists?(application_file)
    raise Thor::Error, "Couldn't find #{subsite}.css!" unless exists?(application_file)
    opts = {:before => /\*=(?!.*\*=).*?$/m}

    raise Thor::Error, "Couldn't find #{subsite}.css!" unless exists?(application_file)

    inject_into_file application_file, opts do
      line = "*= require #{name}\n "
      line = "*\n * #{comments}\n #{line}" if comments
      line
    end
  end

  def inject_dryml_include(name, subsite, comments)
    subsite = "#{subsite}_site" unless subsite=="application"
    application_file = "app/views/taglibs/#{subsite}.dryml"
    pattern          = /\<include gem=.*?\>(?!.*\<include gem=.*?\>).*?\n/m

    raise Thor::Error, "Couldn't find #{subsite}.dryml!" unless exists?(application_file)

    inject_into_file application_file, :after=>pattern do
      line = "\n<include gem='#{name}'/>\n"
      line = "\n<%# #{comments} %>#{line}" if comments
      line
    end
  end


  def exists?(file)
    File.exist?(File.join(destination_root, file))
  end

end
Taglib =
classy_module do

  class_option :user_resource_name,
               :type => :string,
               :desc => "User Resource Name",
               :default => 'user'

end
Subsite =
classy_module do

  include Generators::Hobo::InviteOnly
  include Generators::Hobo::Taglib

  # check_class_collision :suffix => 'SiteController'

  def move_and_generate_files
    if can_mv_application_to_front_site?
      say "Renaming app/views/taglibs/application.dryml to app/views/taglibs/front_site.dryml"  unless options[:quiet]
      unless options[:pretend]
        FileUtils.mv('app/views/taglibs/application.dryml', "app/views/taglibs/front_site.dryml")
        copy_file "application.dryml", 'app/views/taglibs/application.dryml'
      end
    end

    template "site.scss.erb", File.join('app/assets/stylesheets', "#{file_name}.scss")
    copy_file "gitkeep", "app/assets/stylesheets/#{file_name}/.gitkeep"
    template "site.js.erb", File.join('app/assets/javascripts', "#{file_name}.js")
    copy_file "gitkeep", "app/assets/javascripts/#{file_name}/.gitkeep"

    template "controller.rb.erb", File.join('app/controllers', file_name, "#{file_name}_site_controller.rb")

    application "#"
    application "config.assets.precompile += %w(#{file_name}.css #{file_name}.js)"
    application "# Hobo: the #{file_name} subsite loads #{file_name}.css & #{file_name}.js"
  end

  private

  def subsite_name
    class_name
  end

  def can_mv_application_to_front_site?
    File.exist?('app/views/taglibs/application.dryml') && !File.exist?('app/views/taglibs/front_site.dryml')
  end

end
Controller =
classy_module do

  # check_class_collision :suffix => 'Controller'

  class_option :helpers, :type => :boolean,
  :desc => "Generates helper files",
  :default => !Rails.application.config.hobo.dryml_only_templates

  def self.banner
  "rails generate hobo:controller #{self.arguments.map(&:usage).join(' ')}"
  end

  def generate_controller
    if class_path.length == 1 and
      subsite = class_path.first and
	  (
 options[:subsite_controller_is_being_created] or 
        File.exist?(File.join('app/controllers', class_path, "#{subsite}_site_controller.rb"))
      )
      @subsite = subsite.camelize
    end
    template 'controller.rb.erb', File.join('app/controllers',"#{file_path}_controller.rb")
  end

  def generate_helper
    return unless options[:helpers]
    invoke 'helper', [name], options
  end

end
InviteOnly =
classy_module do

  class_option :invite_only,
             :aliases => '-i',
             :type => :boolean,
             :desc => "Add features for an invite only website"

  private

  def invite_only?
    options[:invite_only]
  end

end
ActivationEmail =
classy_module do

  class_option :activation_email,
             :type => :boolean,
             :desc => "Send an email to activate the account"

end