Module: Tomify::Concerns::Api::Helpers
- Included in:
- JSON
- Defined in:
- app/controllers/tomify/concerns/api/helpers.rb
Instance Method Summary collapse
- #create_record ⇒ Object
- #date_range(start_date, end_date = nil) ⇒ Object
- #destroy_record ⇒ Object
- #find_record ⇒ Object
- #find_records ⇒ Object
- #model ⇒ Object
- #model_name ⇒ Object
- #model_param ⇒ Object
- #record_params ⇒ Object
- #search_params ⇒ Object
- #update_record ⇒ Object
Instance Method Details
#create_record ⇒ Object
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_record ⇒ Object
18 19 20 |
# File 'app/controllers/tomify/concerns/api/helpers.rb', line 18 def destroy_record @record.destroy! end |
#find_record ⇒ Object
6 7 8 |
# File 'app/controllers/tomify/concerns/api/helpers.rb', line 6 def find_record @record ||= model.find(params[:id]) end |
#find_records ⇒ Object
2 3 4 |
# File 'app/controllers/tomify/concerns/api/helpers.rb', line 2 def find_records @records ||= model.where(search_params) end |
#model ⇒ Object
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_name ⇒ Object
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_param ⇒ Object
43 44 45 |
# File 'app/controllers/tomify/concerns/api/helpers.rb', line 43 def model_param @model_param ||= controller_name.chomp("s") end |
#record_params ⇒ Object
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_params ⇒ Object
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_record ⇒ Object
14 15 16 |
# File 'app/controllers/tomify/concerns/api/helpers.rb', line 14 def update_record @record.update!(record_params) end |