class Shaf::CreateUriMethods

This class register uri helper methods like: books_uri => /books book_uri(book) => /books/5 new_book_uri => /book/form edit_book_uri(book) => /books/5/edit

And uri template methods: books_uri_template => /books book_uri_template => /books/:id new_book_uri_template => /book/form edit_book_uri_template => /books/:id/edit

Attributes

base[R]
except[R]
name[R]
only[R]
plural_name[R]

Public Class Methods

new(name, base: nil, plural_name: nil, only: nil, except: nil) click to toggle source
# File lib/shaf/extensions/resource_uris.rb, line 109
def initialize(name, base: nil, plural_name: nil, only: nil, except: nil)
  @name = name.to_s
  @base = base&.sub(%r(/\Z), '') || ''
  @plural_name = plural_name&.to_s || Utils::pluralize(name.to_s)
  @only = only
  @except = except
  @added_path_methods = []
end

Public Instance Methods

call() click to toggle source
# File lib/shaf/extensions/resource_uris.rb, line 118
def call
  if plural_name == name
    # Deprecated code path
    # Remove this branch and only keep the `else` behavior when dropping
    # support for this
    register_resource_helper_by_arg
  else
    register_collection_helper
    register_resource_helper
  end

  register_new_resource_helper
  register_edit_resource_helper
  @added_path_methods
end

Private Instance Methods

register(name, template_uri) click to toggle source
# File lib/shaf/extensions/resource_uris.rb, line 183
def register(name, template_uri)
  builder = MethodBuilder.new(name, template_uri)
  @added_path_methods << builder.call
end
register_collection_helper() click to toggle source
# File lib/shaf/extensions/resource_uris.rb, line 138
def register_collection_helper
  return if skip? :collection

  template_uri = "#{base}/#{plural_name}".freeze
  method_name = plural_name
  method_name = "#{name}_collection" if name == @plural_name
  register(method_name, template_uri)
end
register_edit_resource_helper() click to toggle source
# File lib/shaf/extensions/resource_uris.rb, line 176
def register_edit_resource_helper
  return if skip? :edit

  template_uri = "#{base}/#{plural_name}/:id/edit".freeze
  register("edit_#{name}", template_uri)
end
register_new_resource_helper() click to toggle source
# File lib/shaf/extensions/resource_uris.rb, line 169
def register_new_resource_helper
  return if skip? :new

  template_uri = "#{base}/#{name}/form".freeze
  register("new_#{name}", template_uri)
end
register_resource_helper() click to toggle source
# File lib/shaf/extensions/resource_uris.rb, line 147
def register_resource_helper
  return if skip? :resource

  template_uri = "#{base}/#{plural_name}/:id".freeze
  register(name, template_uri)
end
register_resource_helper_by_arg() click to toggle source

If a resource has the same singular and plural names, then this method should be used. It will return the resource uri when a resource is given as argument and the resources uri when no arguments are provided.

# File lib/shaf/extensions/resource_uris.rb, line 157
def register_resource_helper_by_arg
  return register_resource_helper if skip? :collection
  register_collection_helper
  return if skip? :new

  resource_template_uri =   "#{base}/#{plural_name}/:id"
  collection_template_uri = "#{base}/#{plural_name}"

  builder = MethodBuilder.new(name, resource_template_uri, alt_uri: collection_template_uri)
  @added_path_methods << builder.call
end
skip?(name) click to toggle source
# File lib/shaf/extensions/resource_uris.rb, line 188
def skip? name
  if only
    !Array(only).include? name
  elsif except
    Array(except).include? name
  else
    false
  end
end