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
Helpers.
- #destroy_record ⇒ Object
- #find_record ⇒ Object
- #find_records ⇒ Object
- #model ⇒ Object
- #model_class ⇒ Object
- #model_name ⇒ Object
- #model_param ⇒ Object
- #record ⇒ Object
- #record_params ⇒ Object
- #records ⇒ Object
- #recursive_options(association, base, depth) ⇒ Object
- #search_options ⇒ Object
- #serializable_options ⇒ 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
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_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() end |
#model ⇒ Object
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_class ⇒ Object
35 36 37 |
# File 'app/controllers/tomify/concerns/api/helpers.rb', line 35 def model_class @model_class ||= controller_name.classify end |
#model_name ⇒ Object
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_param ⇒ Object
43 44 45 |
# File 'app/controllers/tomify/concerns/api/helpers.rb', line 43 def model_param @model_param ||= model_name.underscore end |
#record ⇒ Object
47 48 49 |
# File 'app/controllers/tomify/concerns/api/helpers.rb', line 47 def record @record ||= find_record end |
#record_params ⇒ Object
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 |
#records ⇒ Object
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 (association, base, depth) return base if depth.zero? = base.dup [:include] ||= [] [:include] << { association => (association, base, depth - 1) } end |
#search_options ⇒ Object
67 68 69 70 71 72 |
# File 'app/controllers/tomify/concerns/api/helpers.rb', line 67 def @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_options ⇒ Object
74 75 76 |
# File 'app/controllers/tomify/concerns/api/helpers.rb', line 74 def @serializable_options ||= {} 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 |