module ActionDispatch::Routing::Mapper::Resources
def arvm_resource(resource_name, options = {}, &block)
def arvm_resource(resource_name, options = {}, &block) except = options.delete(:except) { [] } add_shallow_routes = options.delete(:add_shallow_routes) { true } defaults = options.delete(:defaults) { {} } only_routes = [] nested = shallow_nesting_depth > 0 association_name = options.delete(:association_name) { resource_name.to_s } owner_viewmodel = options.delete(:owner_viewmodel) do if nested parent_resource.name.to_s.singularize.camelize end end defaults = { association_name: association_name, owner_viewmodel: owner_viewmodel, }.merge(defaults) if nested && !except.include?(:create_associated_bulk) collection do post resource_name, controller: resource_name, action: :create_associated_bulk, as: nil, defaults: defaults end end resource resource_name, shallow: true, only: only_routes, defaults: defaults, **options do instance_eval(&block) if block_given? name_route = { as: '' } # Only one route may take the name if nested post('', action: :create_associated, **name_route.extract!(:as)) unless except.include?(:create) get('', action: :show_associated, **name_route.extract!(:as)) unless except.include?(:show) delete('', action: :destroy_associated, **name_route.extract!(:as)) unless except.include?(:destroy) else post('', action: :create, **name_route.extract!(:as)) unless except.include?(:create) get('', action: :show, **name_route.extract!(:as)) unless except.include?(:show) delete('', action: :destroy, **name_route.extract!(:as)) unless except.include?(:destroy) end end # singularly nested resources provide collection accessors at the top level if nested && add_shallow_routes resources resource_name.to_s.pluralize, shallow: true, only: [:show, :destroy] - except do shallow_scope do collection do name_route = { as: '' } # Only one route may take the name post('', action: :create, **name_route.extract!(:as)) unless except.include?(:create) get('', action: :index, **name_route.extract!(:as)) unless except.include?(:index) end end end end end
def arvm_resources(resource_name, options = {}, &block)
def arvm_resources(resource_name, options = {}, &block) except = options.delete(:except) { [] } add_shallow_routes = options.delete(:add_shallow_routes) { true } defaults = options.delete(:defaults) { {} } nested = shallow_nesting_depth > 0 association_name = options.delete(:association_name) { resource_name.to_s } owner_viewmodel = options.delete(:owner_viewmodel) do if nested parent_resource.name.to_s.singularize.camelize end end defaults = { association_name: association_name, owner_viewmodel: owner_viewmodel, }.merge(defaults) only_routes = [] only_routes += [:create] unless nested only_routes += [:show, :destroy] if add_shallow_routes only_routes -= except # Bulk replace if nested && !except.include?(:replace_bulk) collection do post resource_name, controller: resource_name, action: :replace_bulk, as: nil, defaults: defaults end end resources resource_name, shallow: true, only: only_routes, defaults: defaults, **options do instance_eval(&block) if block_given? if nested # Nested controllers also get :append and :disassociate, and alias a top level create. collection do name_route = { as: '' } # Only one route may take the name get('', action: :index_associated, **name_route.extract!(:as)) unless except.include?(:index) put('', action: :append, **name_route.extract!(:as)) unless except.include?(:append) post('', action: :replace, **name_route.extract!(:as)) unless except.include?(:replace) delete('', action: :disassociate_all, **name_route.extract!(:as)) unless except.include?(:disassociate_all) end scope shallow: false do member do delete '', action: :disassociate, as: '' unless except.include?(:disassociate) end end # Add top level `create` route to manipulate existing viewmodels # without providing parent context shallow_scope do collection do name_route = { as: '' } # Only one route may take the name post('', action: :create, **name_route.extract!(:as)) unless except.include?(:create) || !add_shallow_routes get('', action: :index, **name_route.extract!(:as)) unless except.include?(:index) || !add_shallow_routes delete('', action: :destroy, as: :bulk_delete) unless except.include?(:destroy) || !add_shallow_routes end end else collection do get('', action: :index, as: '') unless except.include?(:index) delete('', action: :destroy, as: :bulk_delete) unless except.include?(:destroy) end end end end