Merge branch 'master' into refactor-devise-integration

# Conflicts:
#	app/controllers/registrant/contacts_controller.rb
#	config/routes.rb
This commit is contained in:
Artur Beljajev 2018-07-10 18:09:03 +03:00
commit e5cdb2e8db
79 changed files with 804 additions and 862 deletions

View file

@ -0,0 +1,149 @@
# frozen_string_literal: true
require "action_view"
require "action_controller"
require "action_controller/log_subscriber"
module ActionController
# API Controller is a lightweight version of <tt>ActionController::Base</tt>,
# created for applications that don't require all functionalities that a complete
# \Rails controller provides, allowing you to create controllers with just the
# features that you need for API only applications.
#
# An API Controller is different from a normal controller in the sense that
# by default it doesn't include a number of features that are usually required
# by browser access only: layouts and templates rendering, cookies, sessions,
# flash, assets, and so on. This makes the entire controller stack thinner,
# suitable for API applications. It doesn't mean you won't have such
# features if you need them: they're all available for you to include in
# your application, they're just not part of the default API controller stack.
#
# Normally, +ApplicationController+ is the only controller that inherits from
# <tt>ActionController::API</tt>. All other controllers in turn inherit from
# +ApplicationController+.
#
# A sample controller could look like this:
#
# class PostsController < ApplicationController
# def index
# posts = Post.all
# render json: posts
# end
# end
#
# Request, response, and parameters objects all work the exact same way as
# <tt>ActionController::Base</tt>.
#
# == Renders
#
# The default API Controller stack includes all renderers, which means you
# can use <tt>render :json</tt> and brothers freely in your controllers. Keep
# in mind that templates are not going to be rendered, so you need to ensure
# your controller is calling either <tt>render</tt> or <tt>redirect_to</tt> in
# all actions, otherwise it will return 204 No Content.
#
# def show
# post = Post.find(params[:id])
# render json: post
# end
#
# == Redirects
#
# Redirects are used to move from one action to another. You can use the
# <tt>redirect_to</tt> method in your controllers in the same way as in
# <tt>ActionController::Base</tt>. For example:
#
# def create
# redirect_to root_url and return if not_authorized?
# # do stuff here
# end
#
# == Adding New Behavior
#
# In some scenarios you may want to add back some functionality provided by
# <tt>ActionController::Base</tt> that is not present by default in
# <tt>ActionController::API</tt>, for instance <tt>MimeResponds</tt>. This
# module gives you the <tt>respond_to</tt> method. Adding it is quite simple,
# you just need to include the module in a specific controller or in
# +ApplicationController+ in case you want it available in your entire
# application:
#
# class ApplicationController < ActionController::API
# include ActionController::MimeResponds
# end
#
# class PostsController < ApplicationController
# def index
# posts = Post.all
#
# respond_to do |format|
# format.json { render json: posts }
# format.xml { render xml: posts }
# end
# end
# end
#
# Make sure to check the modules included in <tt>ActionController::Base</tt>
# if you want to use any other functionality that is not provided
# by <tt>ActionController::API</tt> out of the box.
class API < Metal
abstract!
# Shortcut helper that returns all the ActionController::API modules except
# the ones passed as arguments:
#
# class MyAPIBaseController < ActionController::Metal
# ActionController::API.without_modules(:ForceSSL, :UrlFor).each do |left|
# include left
# end
# end
#
# This gives better control over what you want to exclude and makes it easier
# to create an API controller class, instead of listing the modules required
# manually.
def self.without_modules(*modules)
modules = modules.map do |m|
m.is_a?(Symbol) ? ActionController.const_get(m) : m
end
MODULES - modules
end
MODULES = [
AbstractController::Rendering,
UrlFor,
Redirecting,
ApiRendering,
Renderers::All,
ConditionalGet,
BasicImplicitRender,
StrongParameters,
ForceSSL,
DataStreaming,
# Before callbacks should also be executed as early as possible, so
# also include them at the bottom.
AbstractController::Callbacks,
# Append rescue at the bottom to wrap as much as possible.
Rescue,
# Add instrumentations hooks at the bottom, to ensure they instrument
# all the methods properly.
Instrumentation,
# Params wrapper should come before instrumentation so they are
# properly showed in logs
ParamsWrapper
]
MODULES.each do |mod|
include mod
end
ActiveSupport.run_load_hooks(:action_controller_api, self)
ActiveSupport.run_load_hooks(:action_controller, self)
end
end

View file

@ -0,0 +1,16 @@
# frozen_string_literal: true
module ActionController
module ApiRendering
extend ActiveSupport::Concern
included do
include Rendering
end
def render_to_body(options = {})
_process_options(options)
super
end
end
end

View file

@ -0,0 +1,13 @@
# frozen_string_literal: true
module ActionController
module BasicImplicitRender # :nodoc:
def send_action(method, *args)
super.tap { default_render unless performed? }
end
def default_render(*args)
head :no_content
end
end
end

View file

@ -6,7 +6,7 @@ PaperTrail::Version.module_eval do
end
# Store console and rake changes in versions
if defined?(::Rails::Console) || File.basename($PROGRAM_NAME).split(' ').first == 'spring'
if defined?(::Rails::Console)
PaperTrail.whodunnit = "console-#{`whoami`.strip}"
elsif File.basename($PROGRAM_NAME) == 'rake'
# rake username does not work when spring enabled

View file

@ -0,0 +1,3 @@
require_relative 'action_controller/metal/basic_implicit_render'
require_relative 'action_controller/api/api_rendering'
require_relative 'action_controller/api'

View file

@ -1,181 +0,0 @@
namespace :dev do
desc 'Generates dummy data in development environment' \
' (options: [random] for random data generation - slowest)'
task :prime, [:random] => :environment do |t, args|
abort 'Production environment is not supported' if Rails.env.production?
include FactoryBot::Syntax::Methods
PaperTrail.enabled = false
Domain.paper_trail_on!
Contact.paper_trail_on!
with_random_data = args[:random].present?
def create_domain(name:, registrar:, registrant:, account:, price:, reg_time:)
duration = price.duration.sub('mons', 'months').split("\s")
period = duration.first.to_i
period_unit = duration.second[0]
create(:domain,
name: name,
period: period,
period_unit: period_unit,
registered_at: reg_time,
expire_time: reg_time + period.send(duration.second.to_sym),
created_at: reg_time,
updated_at: reg_time,
registrar: registrar,
registrant: registrant)
create(:account_activity,
account: account,
sum: -price.price.amount,
activity_type: AccountActivity::CREATE,
created_at: reg_time,
updated_at: reg_time,
price: price)
end
def generate_default_data
create(:admin_user, username: 'test', password: 'testtest', password_confirmation: 'testtest')
zone = create(:zone, origin: 'test')
registrar = create(:registrar, name: 'test')
registrant = create(:registrant, name: 'test', registrar: registrar)
account = create(:account, registrar: registrar, balance: 1_000_000)
api_user = create(:api_user, username: 'test', password: 'testtest', registrar: registrar)
epp_session = EppSession.new
epp_session.session_id = 'test'
epp_session.user = api_user
epp_session.save!
domain_counter = 1.step
Billing::Price.durations.each do |duration|
Billing::Price.operation_categories.each do |operation_category|
price = create(:price,
price: Money.from_amount(duration.to_i * 10),
valid_from: Time.zone.now - rand(1).months,
valid_to: Time.zone.now + rand(1).months,
duration: duration,
operation_category: operation_category,
zone: zone)
next if operation_category == 'renew'
(rand(3) + 1).times do
create_domain(name: "test#{domain_counter.next}.test",
registrar: registrar,
registrant: registrant,
account: account,
price: price,
reg_time: 1.month.ago)
end
(rand(3) + 1).times do
create_domain(name: "test#{domain_counter.next}.test",
registrar: registrar,
registrant: registrant,
account: account,
price: price,
reg_time: Time.zone.now)
end
end
end
create_domain(name: 'test.test',
registrar: registrar,
registrant: registrant,
account: account,
price: Billing::Price.first,
reg_time: Time.zone.now)
end
def generate_random_data
zone_count = 10
admin_user_count = 5
registrar_count = 50
api_user_count = 10
registrant_count = 50
domain_count = 50
registrars = []
registrants = []
zones = []
registrant_names = [
'John Doe',
'John Roe',
'Jane Doe',
'Jane Roe',
'John Smith',
]
zone_count.times do
origin = SecureRandom.hex[0..(rand(5) + 1)]
zones << create(:zone, origin: origin)
end
zone_origins = zones.collect { |zone| zone.origin }
admin_user_count.times do
uid = SecureRandom.hex[0..(rand(5) + 1)]
create(:admin_user, username: "test#{uid}", password: 'testtest', password_confirmation: 'testtest')
end
registrar_count.times do
uid = SecureRandom.hex[0..(rand(5) + 1)]
registrars << create(:registrar, name: "Acme Ltd. #{uid}")
end
registrars.each do |registrar|
create(:account, registrar: registrar, balance: rand(99999))
api_user_count.times do |i|
create(:api_user, username: "test#{registrar.id}#{i}", password: 'testtest', registrar: registrar)
end
registrant_count.times do |i|
registrants << create(:registrant, name: registrant_names.sample, registrar: registrar)
end
domain_count.times do |i|
name = "test#{registrar.id}#{i}#{rand(99999)}.#{zone_origins.sample}"
period = rand(3) + 1
create(:domain,
name: name,
period: period,
period_unit: 'y',
registered_at: Time.zone.now,
expire_time: Time.zone.now + period.years,
registrar: registrar,
registrant: registrants.sample)
end
end
zones.each do |zone|
Billing::Price.durations.each do |duration|
Billing::Price.operation_categories.each do |operation_category|
create(:price,
price: Money.from_amount(rand(10) + 1),
valid_from: Time.zone.now.beginning_of_day,
valid_to: Time.zone.now + (rand(10) + 1).years,
duration: duration,
operation_category: operation_category,
zone: zone)
end
end
end
end
Setting.address_processing = false
ActiveRecord::Base.transaction do
generate_default_data
generate_random_data if with_random_data
end
end
end