Class: Brick::ModelGenerator
- Includes:
- FancyGets
- Defined in:
- lib/generators/brick/model_generator.rb
Overview
Auto-generates models, controllers, or views
Instance Method Summary collapse
Instance Method Details
#brick_model ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/generators/brick/model_generator.rb', line 23 def brick_model # %%% If Apartment is active, ask which schema they want # Load all models Rails.configuration.eager_load_namespaces.select { |ns| ns < Rails::Application }.each(&:eager_load!) # Generate a list of viable models that can be chosen longest_length = 0 model_info = Hash.new { |h, k| h[k] = {} } tableless = Hash.new { |h, k| h[k] = [] } models = ActiveRecord::Base.descendants.reject do |m| trouble = if m.abstract_class? true elsif !m.table_exists? tableless[m.table_name] << m.name ' (No Table)' else this_f_keys = (model_info[m][:f_keys] = m.reflect_on_all_associations.select { |a| a.macro == :belongs_to }) || [] column_names = (model_info[m][:column_names] = m.columns.map(&:name) - [m.primary_key, 'created_at', 'updated_at', 'deleted_at'] - this_f_keys.map(&:foreign_key)) if column_names.empty? && this_f_keys && !this_f_keys.empty? = ", although #{this_f_keys.length} foreign keys" " (No columns#{})" end end # puts "#{m.name}#{trouble}" if trouble&.is_a?(String) trouble end models.sort! do |a, b| # Sort first to separate namespaced stuff from the rest, then alphabetically is_a_namespaced = a.name.include?('::') is_b_namespaced = b.name.include?('::') if is_a_namespaced && !is_b_namespaced 1 elsif !is_a_namespaced && is_b_namespaced -1 else a.name <=> b.name end end models.each do |m| # Find longest name in the list for future use to show lists on the right side of the screen # Strangely this can't be inlined since it assigns to "len" if longest_length < (len = m.name.length) longest_length = len end end end |