Module: RestingPug::Subject

Defined in:
lib/resting_pug/subject.rb

Overview

Defines everything related to subject basic operations(creation, updating etc)

Used in:

Instance Method Summary collapse

Instance Method Details

#create_subjectObject (protected)

Note:

Override this method to add some custom logic to creating process.

Creates a subject

Examples:

class BooksController < ApplicationController
  include RestingPug::Base

  private
  # Override a create_subject to create only if title is not 'None'
  def create_subject
    @subject = subject_model.create(params_for_create) unless params_for_create[:title] == 'None'
  end
end

Used in:

Use:


83
84
85
# File 'lib/resting_pug/subject.rb', line 83

def create_subject
  @subject = subject_model.create(params_for_create)
end

#destroy_subjectObject (protected)

Note:

Override this method to add some custom logic to destroying process.

Destroys a subject and render nothing or errors

Examples:

class BooksController < ApplicationController
  include RestingPug::Base

  private
  # Override a destroy_subject to destroy only if title is not 'None'
  def destroy_subject
    if @subject.title != 'None' && @subject.destroy
      render_nothing
    else
      render_errors
    end
  end
end

Used in:

Use:


124
125
126
127
128
129
130
# File 'lib/resting_pug/subject.rb', line 124

def destroy_subject
  if @subject.destroy
    render_nothing
  else
    render_errors
  end
end

#fetch_subjectObject (protected)

Note:

Override this method to add some custom logic to fetching process.

Finds a subject by id

Examples:

class BooksController < ApplicationController
  include RestingPug::Base

  private
  # Override a fetch_subject to return a default subject if requested is not found
  def fetch_subject
    @subject = subject_model.find_by_id(params[:id]) || default_subject
  end
end

Used in:

Use:


149
150
151
# File 'lib/resting_pug/subject.rb', line 149

def fetch_subject
  @subject = subject_model.find(params[:id])
end

#fetch_subjectsObject (protected)

Note:

Override this method to add some custom logic to fetching process.

Returns all subjects accessable for API

Examples:

class BooksController < ApplicationController
  include RestingPug::Base

  private
  # Override a fetch_subjects to return subject where title is not 'None'
  def fetch_subjects
    @subjects = subject_model.where.not(title: 'None')
  end
end

Used in:

Use:


167
168
169
# File 'lib/resting_pug/subject.rb', line 167

def fetch_subjects
  @subjects = subject_model.all
end

#subject_modelObject (protected)

Note:

Override this method if you want to use another model.

Gets a subject model from a controller class name

Examples:

class BooksController < ApplicationController
  include RestingPug::Base

  private
  # Override a subject_model to use Novel as a model
  def subject_model
    Novel
  end
end

Used in:


25
26
27
# File 'lib/resting_pug/subject.rb', line 25

def subject_model
  controller_name.classify.constantize
end

#subject_model_symObject (protected)

Note:

Override this method to get another root for params and JSON responses.

Returns a symbol out of the subject model name

Examples:

class BooksController < ApplicationController
  include RestingPug::Base

  private
  # Override a subject_model to use :novel as a symbol used in params and rendering
  def subject_model_sym
    :novel
  end
end

Used in:

Use:


46
47
48
# File 'lib/resting_pug/subject.rb', line 46

def subject_model_sym
  subject_model.to_s.underscore.to_sym
end

#subject_model_sym_pluralObject (protected)

Note:

Override this method to get another root JSON response.

Returns a symbol out of the subject model name(plural)

Examples:

class BooksController < ApplicationController
  include RestingPug::Base

  private
  # Override a subject_model_sym_plural to use :novels as a symbol used in index action
  def subject_model_sym_plural
    :novels
  end
end

Used in:

Use:


64
65
66
# File 'lib/resting_pug/subject.rb', line 64

def subject_model_sym_plural
  subject_model.to_s.pluralize.underscore.to_sym
end

#update_subjectObject (protected)

Note:

Override this method to add some custom logic to updating process.

Updates a subject

Examples:

class BooksController < ApplicationController
  include RestingPug::Base

  private
  # Override a update_subject to update only if title is not 'None'
  def update_subject
    @subject.update(params_for_update) unless params_for_update[:title] == 'None'
  end
end

Used in:

Use:


101
102
103
# File 'lib/resting_pug/subject.rb', line 101

def update_subject
  @subject.update(params_for_update)
end