Module: APIResourceFieldsettable::HelperMethods

Defined in:
app/api/concerns/api_resource_fieldsettable.rb

Instance Method Summary collapse

Instance Method Details

#fieldset(resource = nil, field = nil) ⇒ Object

Getter for the fieldset data


153
154
155
156
157
158
159
160
161
# File 'app/api/concerns/api_resource_fieldsettable.rb', line 153

def fieldset(resource = nil, field = nil)
  if resource.blank?
    @fieldset ||= {}
  elsif field.blank?
    (@fieldset ||= {})[resource] ||= []
  else
    fieldset(resource).include?(field)
  end
end

#fieldset_for(resource, root: false, permitted_fields: [], show_all_permitted_fields_by_default: false, default_fields: []) ⇒ Object

Gets the fields parameters, organize them into a @fieldset hash for model to select certain fields and/or templates to render specified fieldset. Following the URL rules of JSON API: jsonapi.org/format/#fetching-sparse-fieldsets

Params:

resource

Symbol name of resource to receive the fieldset

root

Boolean should this resource take the parameter from fields while no type is specified

permitted_fields

Array of Symbols list of accessible fields used to filter out unpermitted fields, defaults to permit all

default_fields

Array of Symbols list of fields to show by default

show_all_permitted_fields_by_default

Boolean if set to true, @fieldset will be set to all permitted_fields when the current resource's fieldset isn't specified

Example Result:

fieldset_for :user, root: true
fieldset_for :group

# @fieldset => {
#                :user => [:id, :name, :email, :groups],
#                :group => [:id, :name]
#              }

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/api/concerns/api_resource_fieldsettable.rb', line 124

def fieldset_for(resource, root: false, permitted_fields: [], show_all_permitted_fields_by_default: false, default_fields: [])
  @fieldset ||= Hashie::Mash.new
  @meta ||= Hashie::Mash.new

  # put the fields in place
  if params[:fields].is_a? Hash
    @fieldset[resource] = params[:fields][resource] || params[:fields][resource]
  elsif root
    @fieldset[resource] = params[:fields]
  end

  # splits the string into array of symbles
  @fieldset[resource] = @fieldset[resource].present? ? @fieldset[resource].split(',').map(&:to_sym) : default_fields

  # filter out unpermitted fields by intersecting them
  @fieldset[resource] &= permitted_fields if @fieldset[resource].present? && permitted_fields.present?

  # set default fields to permitted_fields if needed
  @fieldset[resource] = permitted_fields if show_all_permitted_fields_by_default && @fieldset[resource].blank? && permitted_fields.present?
end

#set_fieldset(resource, default_fields: [], permitted_fields: []) ⇒ Object

View Helper to set the default and permitted fields


146
147
148
149
150
# File 'app/api/concerns/api_resource_fieldsettable.rb', line 146

def set_fieldset(resource, default_fields: [], permitted_fields: [])
  @fieldset ||= {}
  @fieldset[resource] = default_fields if @fieldset[resource].blank?
  @fieldset[resource] &= permitted_fields
end