Class: Refinery::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/refinery/cli.rb

Constant Summary collapse

OVERRIDES =
{
  :view => {
    :glob => '*.{erb,builder}',
    :dir => 'views',
    :desc => 'view template',
  },
  :controller => {
    :glob => '*.rb',
    :dir => 'controllers',
    :desc => 'controller',
  },
  :model => {
    :glob => '*.rb',
    :dir => 'models',
    :desc => 'model',
  },
  :helper => {
    :glob => '*.rb',
    :dir => 'helpers',
    :desc => 'helper',
  },
  :presenter => {
    :glob => '*.rb',
    :dir => 'presenters',
    :desc => 'presenter',
  },
  :javascript => {
    :glob => '*.js{,.*}',
    :dir => 'assets/javascripts',
    :desc => 'javascript',
  },
  :stylesheet => {
    :glob => '*.css{,.scss}',
    :dir => 'assets/stylesheets',
    :desc => 'stylesheet',
  },
}

Instance Method Summary collapse

Instance Method Details

#override(env) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/refinery/cli.rb', line 54

def override(env)
  OVERRIDES.keys.each do |kind|
    if (which = env[kind.to_s]).present?
      return _override(kind, which)
    end
  end

  puts "You didn't specify anything valid to override. Here are some examples:"
  {
    :view => ['pages/home', 'refinery/pages/home', '**/*menu', '_menu_branch'],
    :javascript => %w(admin refinery/site_bar wymeditor**/{**/}*),
    :stylesheet => %w(home refinery/site_bar),
    :controller => %w(pages),
    :model => %w(page refinery/page),
    :helper => %w(site_bar refinery/site_bar_helper),
    :presenter => %w(refinery/page_presenter)
  }.each do |type, examples|
    examples.each do |example|
      puts "rake refinery:override #{type}=#{example}"
    end
  end
end

#uncrudify(controller, action) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/refinery/cli.rb', line 78

def uncrudify(controller, action)
  unless (controller_name = controller).present? && (action = action).present?
    abort <<-HELPDOC.strip_heredoc
      You didn't specify anything to uncrudify. Here's some examples:
      rake refinery:uncrudify controller=refinery/admin/pages action=create
      rake refinery:uncrudify controller=products action=new
    HELPDOC
  end

  controller_class_name = "#{controller_name}_controller".classify
  begin
    controller_class = controller_class_name.constantize
  rescue NameError
    abort "#{controller_class_name} is not defined"
  end

  crud_lines = Refinery.roots(:'refinery/core').join('lib', 'refinery', 'crud.rb').read
  if (matches = crud_lines.scan(/(\ +)(def #{action}.+?protected)/m).first).present? &&
     (method_lines = "#{matches.last.split(%r{^#{matches.first}end}).first.strip}\nend".split("\n")).many?
    indent = method_lines.second.index %r{[^ ]}
    crud_method = method_lines.join("\n").gsub /^#{" " * indent}/, "  "

    crud_options = controller_class.try(:crudify_options) || {}
    crud_method.gsub! '#{options[:redirect_to_url]}', crud_options[:redirect_to_url].to_s
    crud_method.gsub! '#{options[:conditions].inspect}', crud_options[:conditions].inspect
    crud_method.gsub! '#{options[:title_attribute]}', crud_options[:title_attribute]
    crud_method.gsub! '#{singular_name}', crud_options[:singular_name]
    crud_method.gsub! '#{class_name}', crud_options[:class_name]
    crud_method.gsub! '#{plural_name}', crud_options[:plural_name]
    crud_method.gsub! '\\#{', '#{'

    puts crud_method
  end
end