module BeautifulScaffoldCommonMethods

Private Instance Methods

add_relation() click to toggle source
# File lib/generators/beautiful_scaffold_common_methods.rb, line 224
def add_relation
  myattributes.each{ |attr|
    a,t = attr.split(':')

    foreign_key = a

    if ['references', 'reference'].include?(t)
      foreign_key = "#{a}_id"

      # question (model) belongs_to user (a)
      inject_into_file("app/models/#{engine_name}#{model}.rb", "\n  belongs_to :#{a}, optional: true", :after => "ApplicationRecord")
      inject_into_file("app/models/#{engine_name}#{a}.rb", "\n  has_many :#{model_pluralize}, :dependent => :nullify", :after => "ApplicationRecord")
    end

    inject_into_file("app/models/#{engine_name}#{model}.rb", ":#{foreign_key},", :after => "def self.permitted_attributes\n    return ")
  }
end
attribute_path_i18n(model, attribute) click to toggle source

I18n

# File lib/generators/beautiful_scaffold_common_methods.rb, line 97
def attribute_path_i18n(model, attribute)
  "app.models.#{model}.bs_attributes.#{attribute}"
end
attributes() click to toggle source
# File lib/generators/beautiful_scaffold_common_methods.rb, line 125
def attributes
  # https://raw.github.com/rails/rails/master/railties/lib/rails/generators/generated_attribute.rb
  require 'rails/generators/generated_attribute'
  return myattributes.map{ |a|
    attr, type = a.split(":")
    Rails::Generators::GeneratedAttribute.new(attr, type.to_sym)
  }
end
attributes_without_type() click to toggle source
# File lib/generators/beautiful_scaffold_common_methods.rb, line 158
def attributes_without_type
  newmyattributes = []
  myattributes.each{ |attr|
    a,t = attr.split(':')

    if ['references', 'reference'].include?(t)
      a = a + '_id'
    end

    # Add the typetext to permitted_attr
    if t == 'wysiwyg'
      newmyattributes << "#{a}_typetext"
    end

    newmyattributes << a
  }

  return newmyattributes
end
available_views() click to toggle source
# File lib/generators/beautiful_scaffold_common_methods.rb, line 121
def available_views
  %w(index edit show new _form)
end
beautiful_attr_to_rails_attr() click to toggle source
# File lib/generators/beautiful_scaffold_common_methods.rb, line 134
def beautiful_attr_to_rails_attr #(for_migration = false)
  newmyattributes = []
  myattributes.each{ |attr|
    a,t = attr.split(':')
    newt = t

    # Special columns
    if ['wysiwyg'].include?(t)
      newt = 'text'
    elsif t == 'price'
      newt = 'float'
    elsif ['references', 'reference'].include?(t) # Because Rails generate corrupted files (migrations)
      a = "#{a}_id"
      newt = 'integer:index'
    elsif t == 'color'
      newt = 'string'
    end

    newmyattributes << [a, newt].join(':')
  }

  return newmyattributes
end
engine_camel() click to toggle source
# File lib/generators/beautiful_scaffold_common_methods.rb, line 18
def engine_camel
  options[:mountable_engine].to_s.camelize
end
engine_name() click to toggle source
# File lib/generators/beautiful_scaffold_common_methods.rb, line 14
def engine_name
  engine_opt.blank? ? '' : "#{engine_opt}/"
end
engine_opt() click to toggle source

Engine

# File lib/generators/beautiful_scaffold_common_methods.rb, line 10
def engine_opt
  options[:mountable_engine].to_s.downcase
end
fulltext_attribute() click to toggle source
# File lib/generators/beautiful_scaffold_common_methods.rb, line 178
def fulltext_attribute
  fulltext_field = []
  myattributes.each{ |attr|
    a,t = attr.split(':')
    if ['wysiwyg'].include?(t)
      fulltext_field << a
    end
  }
  return fulltext_field
end
i18n_t_a(model, attribute) click to toggle source
# File lib/generators/beautiful_scaffold_common_methods.rb, line 109
def i18n_t_a(model, attribute)
  "t('#{attribute_path_i18n(model, attribute)}', :default => '#{attribute}')"
end
i18n_t_m(model) click to toggle source
# File lib/generators/beautiful_scaffold_common_methods.rb, line 113
def i18n_t_m(model)
  "t('#{model_path_i18n(model)}', :default => '#{model}')"
end
i18n_t_m_p(model) click to toggle source
# File lib/generators/beautiful_scaffold_common_methods.rb, line 117
def i18n_t_m_p(model)
  "t('#{model_p_path_i18n(model)}', :default => '#{model}')"
end
model() click to toggle source

Models

# File lib/generators/beautiful_scaffold_common_methods.rb, line 62
def model
  model_opt.underscore
end
model_camelize() click to toggle source
# File lib/generators/beautiful_scaffold_common_methods.rb, line 66
def model_camelize
  model.camelize
end
model_class() click to toggle source
# File lib/generators/beautiful_scaffold_common_methods.rb, line 78
def model_class
  model.camelize
end
model_p_path_i18n(model) click to toggle source
# File lib/generators/beautiful_scaffold_common_methods.rb, line 105
def model_p_path_i18n(model)
  "app.models.#{model}.bs_caption_plural"
end
model_path_i18n(model) click to toggle source
# File lib/generators/beautiful_scaffold_common_methods.rb, line 101
def model_path_i18n(model)
  "app.models.#{model}.bs_caption"
end
model_pluralize() click to toggle source
# File lib/generators/beautiful_scaffold_common_methods.rb, line 74
def model_pluralize
  model.pluralize
end
model_with_engine_camelize() click to toggle source
# File lib/generators/beautiful_scaffold_common_methods.rb, line 70
def model_with_engine_camelize
  (engine_name.blank? ? model.camelize : "#{engine_camel}::#{model_camelize}")
end
namespace_alone() click to toggle source
# File lib/generators/beautiful_scaffold_common_methods.rb, line 44
def namespace_alone
  return options[:namespace].to_s.downcase
end
namespace_for_class() click to toggle source

Namespace

# File lib/generators/beautiful_scaffold_common_methods.rb, line 26
def namespace_for_class
  str = namespace_alone
  str = str.camelcase + '::' if not str.blank?
  return str
end
namespace_for_route() click to toggle source
# File lib/generators/beautiful_scaffold_common_methods.rb, line 32
def namespace_for_route
  str = namespace_alone
  str = str.downcase + '_' if not str.blank?
  return str
end
namespace_for_url() click to toggle source
# File lib/generators/beautiful_scaffold_common_methods.rb, line 38
def namespace_for_url
  str = namespace_alone
  str = str.downcase + '/' if not str.blank?
  return str
end
plural_table_name() click to toggle source

Table

# File lib/generators/beautiful_scaffold_common_methods.rb, line 86
def plural_table_name
  model_pluralize
end
regexp_an_string() click to toggle source
# File lib/generators/beautiful_scaffold_common_methods.rb, line 54
def regexp_an_string
  '\s*\R\s*'
end
render_partial(path) click to toggle source
# File lib/generators/beautiful_scaffold_common_methods.rb, line 48
def render_partial(path)
  source  = File.expand_path(find_in_source_paths(path.to_s))
  result = ERB.new(::File.binread(source), trim_mode: '-').result(binding)
  return result
end
require_gems() click to toggle source
# File lib/generators/beautiful_scaffold_common_methods.rb, line 193
def require_gems
  gems = {
    'will_paginate' => nil, # v 3.1.5
    'ransack' => nil, #'2.3.2',
    'jquery-ui-rails' => nil,
    'prawn' => nil, #'2.1.0',
    'prawn-table' => nil, #'0.2.2',
    'sanitize' => nil,
    #'twitter-bootstrap-rails' => '3.2.2', # Bootstrap 3 for Rails 6+
    'bootstrap' => '~> 5.1.0', # Bootstrap 4 for Rails 6+
    'font-awesome-sass' => '~> 5.13.0',
    'momentjs-rails' => '>= 2.9.0',
    'bootstrap4-datetime-picker-rails' => nil,
    'jquery-rails' => '4.3.1',
    'jstree-rails-4' => '3.3.8'
  }

  # Si engine il faut mettre les gems dans le gemspec et faire le require
  if !Dir.glob('./*.gemspec').empty?
    puts "============> Engine : You must add gems to your main app \n #{gems.to_a.map{ |a| "gem '#{a[0]}'#{(a[1].nil? ? '' : ", '#{a[1]}'")} " }.join("\n")}"
  end

  gemfile_content = File.read('Gemfile')
  gems.each{ |gem_to_add, version|
    # Bug add at every times, need to check if already present
    if !gemfile_content.include?(gem_to_add)
      gem(gem_to_add, version)
    end
  }
end
richtext_type() click to toggle source
# File lib/generators/beautiful_scaffold_common_methods.rb, line 189
def richtext_type
  return ["html","text"]
end
singular_table_name() click to toggle source
# File lib/generators/beautiful_scaffold_common_methods.rb, line 89
def singular_table_name
  model
end