class Bootstrap::Generators::StrapifyGenerator

Public Class Methods

new(args, *options) click to toggle source
Calls superclass method
# File lib/generators/bootstrap/strapify/strapify_generator.rb, line 15
def initialize(args, *options)
  super(args, *options)
  initialize_views_variables
end

Public Instance Methods

copy_views() click to toggle source
# File lib/generators/bootstrap/strapify/strapify_generator.rb, line 20
def copy_views
  generate_views
end

Protected Instance Methods

columns() click to toggle source
# File lib/generators/bootstrap/strapify/strapify_generator.rb, line 61
def columns
  retrieve_columns.reject {|c| excluded?(c.name) }.map do |c|
    new_attribute(c.name, c.type.to_s)
  end
end
controller_routing_path() click to toggle source
# File lib/generators/bootstrap/strapify/strapify_generator.rb, line 33
def controller_routing_path
  ActiveModel::Naming.route_key(@model_name.constantize)
end
excluded?(name) click to toggle source
# File lib/generators/bootstrap/strapify/strapify_generator.rb, line 82
def excluded?(name)
  excluded_columns_names.include?(name) ||
  excluded_columns_pattern.any? {|p| name =~ p } ||
  excluded_columns.include?(name)
end
excluded_columns() click to toggle source
# File lib/generators/bootstrap/strapify/strapify_generator.rb, line 78
def excluded_columns
  options['excluded_columns']||[]
end
excluded_columns_names() click to toggle source
# File lib/generators/bootstrap/strapify/strapify_generator.rb, line 67
def excluded_columns_names
  %w[_id _type id created_at updated_at]
end
excluded_columns_pattern() click to toggle source
# File lib/generators/bootstrap/strapify/strapify_generator.rb, line 71
def excluded_columns_pattern
  [
    /.*_checksum/,
    /.*_count/,
  ]
end
ext() click to toggle source
# File lib/generators/bootstrap/strapify/strapify_generator.rb, line 139
def ext
  ::Rails.application.config.generators.options[:rails][:template_engine] || :erb
end
extract_modules(name) click to toggle source
# File lib/generators/bootstrap/strapify/strapify_generator.rb, line 111
def extract_modules(name)
  modules = name.include?('/') ? name.split('/') : name.split('::')
  name    = modules.pop
  path    = modules.map { |m| m.underscore }
  file_path = (path + [name.underscore]).join('/')
  nesting = modules.map { |m| m.camelize }.join('::')
  [name, path, file_path, nesting, modules.size]
end
generate_erb(views) click to toggle source
# File lib/generators/bootstrap/strapify/strapify_generator.rb, line 133
def generate_erb(views)
  views.each do |template_name, output_path|
    template template_name, output_path
  end
end
generate_views() click to toggle source
# File lib/generators/bootstrap/strapify/strapify_generator.rb, line 120
def generate_views
  options.engine == generate_erb(selected_views)
end
initialize_views_variables() click to toggle source
# File lib/generators/bootstrap/strapify/strapify_generator.rb, line 26
def initialize_views_variables
  @base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(controller_path)
  @controller_routing_path = @controller_file_path.gsub(/\//, '_')
  @model_name = @controller_class_nesting + "::#{@base_name.singularize.camelize}" unless @model_name
  @model_name = @model_name.camelize
end
model_name() click to toggle source
# File lib/generators/bootstrap/strapify/strapify_generator.rb, line 41
def model_name
  @model_name
end
new_attribute(name, type) click to toggle source
# File lib/generators/bootstrap/strapify/strapify_generator.rb, line 100
def new_attribute(name, type)
  ::Rails::Generators::GeneratedAttribute.new(name, type)
end
plural_model_name() click to toggle source
# File lib/generators/bootstrap/strapify/strapify_generator.rb, line 45
def plural_model_name
  @model_name.pluralize
end
plural_resource_name() click to toggle source
# File lib/generators/bootstrap/strapify/strapify_generator.rb, line 57
def plural_resource_name
  resource_name.pluralize
end
rescue_block(exception=Exception) { || ... } click to toggle source
# File lib/generators/bootstrap/strapify/strapify_generator.rb, line 104
def rescue_block(exception=Exception)
  yield if block_given?
rescue exception => e
  say e.message, :red
  exit
end
resource_name() click to toggle source
# File lib/generators/bootstrap/strapify/strapify_generator.rb, line 53
def resource_name
  @model_name.demodulize.underscore
end
resource_name_with_prefix() click to toggle source
# File lib/generators/bootstrap/strapify/strapify_generator.rb, line 49
def resource_name_with_prefix
  "@#{resource_name}"
end
retrieve_columns() click to toggle source
# File lib/generators/bootstrap/strapify/strapify_generator.rb, line 88
def retrieve_columns
  if defined?(ActiveRecord) == "constant" && ActiveRecord.class == Module
    rescue_block ActiveRecord::StatementInvalid do
      @model_name.constantize.columns
    end
  else
    rescue_block do
      @model_name.constantize.fields.map {|c| c[1] }
    end
  end
end
selected_views() click to toggle source
# File lib/generators/bootstrap/strapify/strapify_generator.rb, line 124
def selected_views
  {
    "index.html.#{ext}"                 => File.join('app/views', @controller_file_path, "index.html.#{ext}"),
    "new.html.#{ext}"                   => File.join('app/views', @controller_file_path, "new.html.#{ext}"),
    "edit.html.#{ext}"                  => File.join('app/views', @controller_file_path, "edit.html.#{ext}"),
    "show.html.#{ext}"                  => File.join('app/views', @controller_file_path, "show.html.#{ext}")
  }
end
singular_controller_routing_path() click to toggle source
# File lib/generators/bootstrap/strapify/strapify_generator.rb, line 37
def singular_controller_routing_path
  ActiveModel::Naming.singular_route_key(@model_name.constantize)
end