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



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

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_params)
end

#modelObject



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

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

#model_nameObject



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

def model_name
  @model_name ||= controller_name.chomp("s").titleize
end

#model_paramObject



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

def model_param
  @model_param ||= controller_name.chomp("s")
end

#record_paramsObject



22
23
24
# File 'app/controllers/tomify/concerns/api/helpers.rb', line 22

def record_params
  params.require(model_param).permit(model.default_params)
end

#search_paramsObject



26
27
28
29
30
31
# File 'app/controllers/tomify/concerns/api/helpers.rb', line 26

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

#update_recordObject



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

def update_record
  @record.update!(record_params)
end