class Fae::BaseGenerator

Public Instance Methods

check_template_support() click to toggle source
# File lib/generators/fae/base_generator.rb, line 15
def check_template_support
  supported_templates = ['slim']
  raise "Fae::UnsupportedTemplate: the template engine you defined isn't supported" unless supported_templates.include?(options.template)
end
set_globals() click to toggle source
# File lib/generators/fae/base_generator.rb, line 20
def set_globals
  if attributes.present?
    attributes.each do |arg|
      # :image and :file args get used to generate association defs and form elements
      # we don't want them in attributes_flat or attribute_names as they are not real model generator field options
      if is_attachment(arg)
        @@attachments << arg
      else
        @@attributes_flat << "#{arg.name}:#{arg.type}"
      end

      if arg.name['_id'] || arg.type.to_s == 'references'
        @@association_names << arg.name.gsub('_id', '')
      elsif !is_attachment(arg)
        @@attribute_names << arg.name
      end
      @@has_position = true if arg.name === 'position'
    end

    @@attributes_flat = @@attributes_flat.uniq.join(' ')
    @@association_names.uniq!
    @@attribute_names.uniq!
    @@attachments.uniq!
  end
end

Private Instance Methods

add_route() click to toggle source
# File lib/generators/fae/base_generator.rb, line 76
    def add_route
      inject_into_file "config/routes.rb", after: "namespace :#{options.namespace} do\n" do <<-RUBY
    resources :#{plural_file_name}
RUBY
      end
    end
generate_controller_file() click to toggle source
# File lib/generators/fae/base_generator.rb, line 58
def generate_controller_file
  @attachments = @@attachments
  template "controllers/scaffold_controller.rb", "app/controllers/#{options.namespace}/#{file_name.pluralize}_controller.rb"
end
generate_model() click to toggle source

Generator Methods

# File lib/generators/fae/base_generator.rb, line 50
def generate_model
  generate "model #{file_name} #{@@attributes_flat}"
  inject_concern
  inject_display_field_to_model
  inject_model_attachments
  inject_position_scope
end
generate_view_files() click to toggle source
# File lib/generators/fae/base_generator.rb, line 63
def generate_view_files
  @toggle_attrs = set_toggle_attrs
  @form_attrs = set_form_attrs
  @association_names = @@association_names
  @attachments = @@attachments
  @has_position = @@has_position
  @display_field = @@display_field
  template "views/index.html.#{options.template}", "app/views/#{options.namespace}/#{plural_file_name}/index.html.#{options.template}"
  template "views/_form.html.#{options.template}", "app/views/#{options.namespace}/#{plural_file_name}/_form.html.#{options.template}"
  copy_file "views/new.html.#{options.template}", "app/views/#{options.namespace}/#{plural_file_name}/new.html.#{options.template}"
  copy_file "views/edit.html.#{options.template}", "app/views/#{options.namespace}/#{plural_file_name}/edit.html.#{options.template}"
end
inject_concern() click to toggle source
# File lib/generators/fae/base_generator.rb, line 94
    def inject_concern
      inject_into_file "app/models/#{file_name}.rb", after: /(ActiveRecord::Base|ApplicationRecord)\n/ do <<-RUBY
  include Fae::BaseModelConcern\n
RUBY
      end
    end
inject_display_field_to_model() click to toggle source
# File lib/generators/fae/base_generator.rb, line 101
    def inject_display_field_to_model
      if @@attribute_names.include? 'name'
        @@display_field = 'name'
      elsif @@attribute_names.include? 'title'
        @@display_field = 'title'
      end

      inject_into_file "app/models/#{file_name}.rb", after: "include Fae::BaseModelConcern\n" do <<-RUBY
  def fae_display_field
    #{@@display_field}
  end
RUBY
      end

    end
inject_model_attachments() click to toggle source
# File lib/generators/fae/base_generator.rb, line 127
    def inject_model_attachments
      return if @@attachments.blank?
      @@attachments.each do |attachment|
        if attachment.type == :image
          inject_into_file "app/models/#{file_name}.rb", after: "include Fae::BaseModelConcern\n" do
            <<-RUBY
  has_fae_image :#{attachment.name}\n
RUBY
          end
        elsif attachment.type == :file
          inject_into_file "app/models/#{file_name}.rb", after: "include Fae::BaseModelConcern\n" do
            <<-RUBY
  has_fae_file :#{attachment.name}\n
RUBY
          end
        end
      end
    end
inject_nav_item() click to toggle source
# File lib/generators/fae/base_generator.rb, line 146
def inject_nav_item
  line = "item('#{plural_file_name.humanize.titlecase}', path: #{options.namespace}_#{plural_file_name}_path),\n\s\s\s\s\s\s\s\s"
  inject_into_file 'app/models/concerns/fae/navigation_concern.rb', line, before: '# scaffold inject marker'
end
inject_position_scope() click to toggle source
# File lib/generators/fae/base_generator.rb, line 117
    def inject_position_scope
      if @@has_position
        inject_into_file "app/models/#{file_name}.rb", after: "include Fae::BaseModelConcern\n" do <<-RUBY
\n  acts_as_list add_new_at: :top
  default_scope { order(:position) }\n
RUBY
        end
      end
    end
is_attachment(arg) click to toggle source
# File lib/generators/fae/base_generator.rb, line 151
def is_attachment(arg)
  [:image,:file].include?(arg.type)
end
set_form_attrs() click to toggle source
# File lib/generators/fae/base_generator.rb, line 89
def set_form_attrs
  rejected_attrs = ['position', 'on_stage', 'on_prod']
  @@attribute_names.delete_if { |i| rejected_attrs.include?(i)}
end
set_toggle_attrs() click to toggle source

Helper Methods

# File lib/generators/fae/base_generator.rb, line 85
def set_toggle_attrs
  ['active', 'on_stage', 'on_prod'] & @@attribute_names
end