Class: Refinery::CLI

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

Constant Summary

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 => '*.{s,}css',
    :dir => 'assets/stylesheets',
    :desc => 'stylesheet',
  },
}

Instance Method Summary collapse

Instance Method Details

#override(env) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'core/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

  handle_invalid_override_input
end

#override_list(env) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'core/lib/refinery/cli.rb', line 65

def override_list(env)
  OVERRIDES.keys.each do |kind|
    which = env[kind.to_s] || (kind if env['type'] == kind.to_s)
    if which.present? && @override_kind = OVERRIDES[kind]
      matcher = [
        "{refinery#{File::SEPARATOR},}", (which.presence || "**/*"), @override_kind[:glob]
      ].flatten.join

      puts find_relative_matches(matcher).sort.join("\n")
      return
    end
  end

  handle_invalid_override_list_input
end

#uncrudify(controller, action) ⇒ Object



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
112
113
114
115
# File 'core/lib/refinery/cli.rb', line 82

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