Module: Tomify::Concerns::Api::Helpers

Included in:
JSON
Defined in:
app/controllers/tomify/concerns/api/helpers.rb

Instance Method Summary collapse

Instance Method Details

#create_recordObject



10
11
12
# File 'app/controllers/tomify/concerns/api/helpers.rb', line 10

def create_record
  @record ||= model.create!(record_params)
end

#date_range(start_date, end_date = nil) ⇒ Object

Helpers



23
24
25
26
27
28
# File 'app/controllers/tomify/concerns/api/helpers.rb', line 23

def date_range(start_date, end_date = nil)
  start_date = DateTime.strptime(start_date, "%D")
  end_date = end_date ? DateTime.strptime(end_date, "%D") : DateTime.now

  (start_date.beginning_of_day)..(end_date.end_of_day)
end

#destroy_recordObject



18
19
20
# File 'app/controllers/tomify/concerns/api/helpers.rb', line 18

def destroy_record
  record.destroy!
end

#find_recordObject



6
7
8
# File 'app/controllers/tomify/concerns/api/helpers.rb', line 6

def find_record
  @record ||= model.find(params[:id])
end

#find_recordsObject



2
3
4
# File 'app/controllers/tomify/concerns/api/helpers.rb', line 2

def find_records
  @records ||= model.where(search_options)
end

#modelObject



30
31
32
33
# File 'app/controllers/tomify/concerns/api/helpers.rb', line 30

def model
  @model ||= model_class.constantize rescue nil
  @model ||= "Tomify::#{model_class}".constantize
end

#model_classObject



35
36
37
# File 'app/controllers/tomify/concerns/api/helpers.rb', line 35

def model_class
  @model_class ||= controller_name.classify
end

#model_nameObject



39
40
41
# File 'app/controllers/tomify/concerns/api/helpers.rb', line 39

def model_name
  @model_name ||= model_class.split("::").first.titleize
end

#model_paramObject



43
44
45
# File 'app/controllers/tomify/concerns/api/helpers.rb', line 43

def model_param
  @model_param ||= model_name.underscore
end

#recordObject



47
48
49
# File 'app/controllers/tomify/concerns/api/helpers.rb', line 47

def record
  @record ||= find_record
end

#record_paramsObject



55
56
57
# File 'app/controllers/tomify/concerns/api/helpers.rb', line 55

def record_params
  @record_params ||= params.require(model_param).permit(permitted_attributes)
end

#recordsObject



51
52
53
# File 'app/controllers/tomify/concerns/api/helpers.rb', line 51

def records
  @records ||= find_records
end

#recursive_options(association, base, depth) ⇒ Object



59
60
61
62
63
64
65
# File 'app/controllers/tomify/concerns/api/helpers.rb', line 59

def recursive_options(association, base, depth)
  return base if depth.zero?
  options = base.dup
  options[:include] ||= []
  options[:include] << { association => recursive_options(association, base, depth - 1) }
  options
end

#search_optionsObject



67
68
69
70
71
72
# File 'app/controllers/tomify/concerns/api/helpers.rb', line 67

def search_options
  @search_options = {}
  @search_options[:created_at] = date_range(params[:created_at]) if params[:created_at]
  @search_options[:updated_at] = date_range(params[:updated_at]) if params[:updated_at]
  @search_options
end

#serializable_optionsObject



74
75
76
# File 'app/controllers/tomify/concerns/api/helpers.rb', line 74

def serializable_options
  @serializable_options ||= {}
end

#update_recordObject



14
15
16
# File 'app/controllers/tomify/concerns/api/helpers.rb', line 14

def update_record
  record.update!(record_params)
end