class Noticed::Generators::ModelGenerator

Public Instance Methods

add_not_nullable() click to toggle source
# File lib/generators/noticed/model_generator.rb, line 25
def add_not_nullable
  migration_path = Dir.glob(Rails.root.join("db/migrate/*")).max_by { |f| File.mtime(f) }

  # Force is required because null: false already exists in the file and Thor isn't smart enough to tell the difference
  insert_into_file migration_path, after: "t.string :type", force: true do
    ", null: false"
  end
end
add_noticed_model() click to toggle source
# File lib/generators/noticed/model_generator.rb, line 21
def add_noticed_model
  inject_into_class model_path, class_name, "  include Noticed::Model\n"
end
done() click to toggle source
# File lib/generators/noticed/model_generator.rb, line 34
def done
  readme "README" if behavior == :invoke
end
generate_notification() click to toggle source
# File lib/generators/noticed/model_generator.rb, line 17
def generate_notification
  generate :model, name, "recipient:references{polymorphic}", "type", params_column, "read_at:datetime:index", *attributes
end

Private Instance Methods

current_adapter() click to toggle source
# File lib/generators/noticed/model_generator.rb, line 54
def current_adapter
  if ActiveRecord::Base.respond_to?(:connection_db_config)
    ActiveRecord::Base.connection_db_config.adapter
  else
    ActiveRecord::Base.connection_config[:adapter]
  end
end
model_path() click to toggle source
# File lib/generators/noticed/model_generator.rb, line 40
def model_path
  @model_path ||= File.join("app", "models", "#{file_path}.rb")
end
params_column() click to toggle source
# File lib/generators/noticed/model_generator.rb, line 44
def params_column
  case current_adapter
  when "postgresql"
    "params:jsonb"
  else
    # MySQL and SQLite both support json
    "params:json"
  end
end