Class: Cuprum::Rails::Resource

Inherits:
Collections::Resource
  • Object
show all
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

Constructor Details

#initialize(entity_class: nil, name: nil, qualified_name: nil, singular_name: nil, **options) ⇒ Resource

Returns a new instance of Resource.

Parameters:

  • entity_class (Class, String) (defaults to: nil)

    the class of entity represented by the resource.

  • name (String) (defaults to: nil)

    the name of the resource.

  • qualified_name (String) (defaults to: nil)

    a scoped name for the resource.

  • routes (Cuprum::Rails::Routes)

    the routes defined for the resource.

  • singular_name (String) (defaults to: nil)

    the name of an entity in the resource.

  • options (Hash)

    additional options for the resource.

Options Hash (**options):

  • actions (Array, Set)

    the defined actions for the resource.

  • base_path (String)

    the base url for the resource.

  • default_order (Hash)

    the default ordering for the resource items.

  • permitted_attributes (Array)

    list of attributes that can be set or changed by resourceful actions.

  • primary_key_name (String)

    the name of the primary key attribute. Defaults to ‘id’.

  • plural (Boolean)

    if true, the resource represents a plural resource. Defaults to true. Can also be specified as :singular.



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

#actionsSet

Returns the defined actions for the resource.

Returns:

  • (Set)

    the defined actions for the resource.



54
55
56
# File 'lib/cuprum/rails/resource.rb', line 54

def actions
  @actions ||= Set.new(options.fetch(:actions, default_actions).map(&:to_s))
end

#ancestorsArray<Resource>

Returns the resource’s ancestors, starting with the resource itself.

Returns:

  • (Array<Resource>)

    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_pathString

Returns the base url for the resource.

Returns:

  • (String)

    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 ||=
    options
      .fetch(:base_path) { default_base_path }
      .to_s
end

#default_orderHash

Returns the default ordering for the resource items.

Returns:

  • (Hash)

    the default ordering for the resource items.



84
85
86
# File 'lib/cuprum/rails/resource.rb', line 84

def default_order
  @default_order ||= options.fetch(:default_order, {})
end

#each_ancestor {|_self| ... } ⇒ Object

Enumerates the resource’s ancestors, starting with the resource itself.

Yields:

  • (_self)

Yield Parameters:



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

#parentCuprum::Rails::Resource

Returns the parent resource, if any.

Returns:



104
105
106
# File 'lib/cuprum/rails/resource.rb', line 104

def parent
  @parent ||= @options.fetch(:parent, nil)
end

#permitted_attributesArray

Returns list of attributes that can be set or changed by resourceful actions.

Returns:

  • (Array)

    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 ||= options.fetch(:permitted_attributes, nil)
end

#primary_key_nameString Also known as: primary_key

Returns the name of the primary key attribute. Defaults to ‘id’.

Returns:

  • (String)

    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 ||=
    options
      .fetch(:primary_key_name) { entity_class.primary_key }
      .to_s
end

#primary_key_typeClass, Stannum::Constraint

Returns the type of the primary key attribute. Defaults to Integer.

Returns:

  • (Class, Stannum::Constraint)

    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 =
    options
      .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.

Parameters:

  • wildcards (Hash) (defaults to: {})

    The wildcard values to use in the routes.

Returns:



127
128
129
# File 'lib/cuprum/rails/resource.rb', line 127

def routes(wildcards: {})
  routes_without_wildcards.with_wildcards(wildcards)
end