Class: Cuprum::Rails::Resource
- Inherits:
-
Collections::Resource
- Object
- Collections::Resource
- Cuprum::Rails::Resource
- Defined in:
- lib/cuprum/rails/resource.rb
Overview
Value object representing a controller resource.
Constant Summary collapse
- PLURAL_ACTIONS =
Default actions for a plural resource.
%w[create destroy edit index new show update].freeze
- SINGULAR_ACTIONS =
Default actions for a singular resource.
%w[create destroy edit new show update].freeze
Instance Method Summary collapse
-
#actions ⇒ Set
The defined actions for the resource.
-
#ancestors ⇒ Array<Resource>
The resource’s ancestors, starting with the resource itself.
-
#base_path ⇒ String
The base url for the resource.
-
#default_order ⇒ Hash
The default ordering for the resource items.
-
#each_ancestor {|_self| ... } ⇒ Object
Enumerates the resource’s ancestors, starting with the resource itself.
-
#initialize(entity_class: nil, name: nil, qualified_name: nil, singular_name: nil, **options) ⇒ Resource
constructor
A new instance of Resource.
-
#parent ⇒ Cuprum::Rails::Resource
The parent resource, if any.
-
#permitted_attributes ⇒ Array
List of attributes that can be set or changed by resourceful actions.
-
#primary_key_name ⇒ String
(also: #primary_key)
The name of the primary key attribute.
-
#primary_key_type ⇒ Class, Stannum::Constraint
The type of the primary key attribute.
-
#routes(wildcards: {}) ⇒ Cuprum::Rails::Routes
Generates the routes for the resource and injects the given wildcards.
Constructor Details
#initialize(entity_class: nil, name: nil, qualified_name: nil, singular_name: nil, **options) ⇒ Resource
Returns a new instance of Resource.
45 46 47 48 49 50 51 |
# File 'lib/cuprum/rails/resource.rb', line 45 def initialize(routes: nil, **params) validate_permitted_attributes(params[:permitted_attributes]) super(**params) @routes = routes end |
Instance Method Details
#actions ⇒ Set
Returns the defined actions for the resource.
54 55 56 |
# File 'lib/cuprum/rails/resource.rb', line 54 def actions @actions ||= Set.new(.fetch(:actions, default_actions).map(&:to_s)) end |
#ancestors ⇒ Array<Resource>
Returns the resource’s ancestors, starting with the resource itself.
60 61 62 |
# File 'lib/cuprum/rails/resource.rb', line 60 def ancestors each_ancestor.to_a end |
#base_path ⇒ String
Returns the base url for the resource.
65 66 67 68 69 70 |
# File 'lib/cuprum/rails/resource.rb', line 65 def base_path @base_path ||= .fetch(:base_path) { default_base_path } .to_s end |
#default_order ⇒ Hash
Returns the default ordering for the resource items.
84 85 86 |
# File 'lib/cuprum/rails/resource.rb', line 84 def default_order @default_order ||= .fetch(:default_order, {}) end |
#each_ancestor {|_self| ... } ⇒ Object
Enumerates the resource’s ancestors, starting with the resource itself.
75 76 77 78 79 80 81 |
# File 'lib/cuprum/rails/resource.rb', line 75 def each_ancestor(&block) return enum_for(:each_ancestor) unless block_given? parent&.each_ancestor(&block) yield self end |
#parent ⇒ Cuprum::Rails::Resource
Returns the parent resource, if any.
104 105 106 |
# File 'lib/cuprum/rails/resource.rb', line 104 def parent @parent ||= @options.fetch(:parent, nil) end |
#permitted_attributes ⇒ Array
Returns list of attributes that can be set or changed by resourceful actions.
90 91 92 |
# File 'lib/cuprum/rails/resource.rb', line 90 def permitted_attributes @permitted_attributes ||= .fetch(:permitted_attributes, nil) end |
#primary_key_name ⇒ String Also known as: primary_key
Returns the name of the primary key attribute. Defaults to ‘id’.
95 96 97 98 99 100 |
# File 'lib/cuprum/rails/resource.rb', line 95 def primary_key_name @primary_key_name ||= .fetch(:primary_key_name) { entity_class.primary_key } .to_s end |
#primary_key_type ⇒ Class, Stannum::Constraint
Returns the type of the primary key attribute. Defaults to Integer.
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/cuprum/rails/resource.rb', line 110 def primary_key_type @primary_key_type = .fetch(:primary_key_type) do key = entity_class.primary_key column = entity_class.columns.find { |col| col.name == key } STRING_COLUMN_TYPES.include?(column.type) ? String : Integer end # rubocop:disable Style/MultilineBlockChain .then { |value| value.is_a?(String) ? value.constantize : value } end |
#routes(wildcards: {}) ⇒ Cuprum::Rails::Routes
Generates the routes for the resource and injects the given wildcards.
127 128 129 |
# File 'lib/cuprum/rails/resource.rb', line 127 def routes(wildcards: {}) routes_without_wildcards.with_wildcards(wildcards) end |