Class: Api::V1::CategoriesController
- Inherits:
-
BaseController
- Object
- ActionController::Base
- BaseController
- Api::V1::CategoriesController
- Defined in:
- app/controllers/api/v1/categories_controller.rb
Instance Method Summary collapse
Instance Method Details
#create ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'app/controllers/api/v1/categories_controller.rb', line 82 def create parent_id = params[:parent_id] begin ActiveRecord::Base.transaction do category = Category.new( description: params[:description].strip ) if params[:internal_identifier].present? category.internal_identifier = params[:internal_identifier].strip else category.internal_identifier = Category.generate_unique_iid(params[:description].strip) end category.save! if parent_id and parent_id != 'No Parent' parent = Category.find(parent_id) if parent category.move_to_child_of(parent) end end EntityPartyRole.create(party: current_user.party.dba_organization, role_type: RoleType.iid('dba_org'), entity_record: category) render json: {success: true, category: category.to_data_hash} end rescue ActiveRecord::RecordInvalid => invalid Rails.logger.error invalid.record.errors. render json: {:success => false, :message => invalid.record.errors..join('</br>')} rescue => ex Rails.logger.error ex. Rails.logger.error ex.backtrace.join("\n") ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier render json: {:success => false, :message => "Error creating record"} end end |
#destroy ⇒ Object
155 156 157 158 159 160 161 |
# File 'app/controllers/api/v1/categories_controller.rb', line 155 def destroy category = Category.find(params[:id]) category.destroy render json: {success: true} end |
#index ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/controllers/api/v1/categories_controller.rb', line 5 def index sort = nil dir = nil limit = nil start = nil unless params[:sort].blank? sort_hash = params[:sort].blank? ? {} : Hash.symbolize_keys(JSON.parse(params[:sort]).first) sort = sort_hash[:property] || 'description' dir = sort_hash[:direction] || 'ASC' limit = params[:limit] || 25 start = params[:start] || 0 end query_filter = params[:query_filter].blank? ? {} : JSON.parse(params[:query_filter]).symbolize_keys # hook method to apply any scopes passed via parameters to this api categories = Category.apply_filters(query_filter) # scope by dba_organizations if there are no parties passed as filters dba_organizations = [current_user.party.dba_organization] dba_organizations = dba_organizations.concat(current_user.party.dba_organization.child_dba_organizations) categories = categories.scope_by_dba_organization(dba_organizations) respond_to do |format| format.json do if sort and dir categories = categories.order("#{sort} #{dir}") end total_count = categories.count if start and limit categories = categories.offset(start).limit(limit) end render :json => {success: true, total_count: total_count, categories: categories.collect { |item| item.to_data_hash }} end format.tree do if params[:parent_id] render :json => {success: true, categories: Category.find(params[:parent_id]).children_to_tree_hash} else nodes = [].tap do |nodes| categories.roots.each do |root| nodes.push(root.to_tree_hash) end end render :json => {success: true, categories: nodes} end end format.all_representation do if params[:parent_id].present? render :json => {success: true, categories: BizTxnAcctRoot.to_all_representation(Category.find(params[:parent_id]))} else render :json => {success: true, categories: BizTxnAcctRoot.to_all_representation(nil, [], 0, categories.roots)} end end end end |
#show ⇒ Object
76 77 78 79 80 |
# File 'app/controllers/api/v1/categories_controller.rb', line 76 def show category = Category.find(params[:id]) render json: {category: category.to_data_hash} end |
#update ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'app/controllers/api/v1/categories_controller.rb', line 127 def update category = Category.find(params[:id]) begin ActiveRecord::Base.transaction do if params[:description].present? category.description = params[:description].strip end if params[:internal_identifier].present? category.internal_identifier = params[:internal_identifier].strip end category.save! render json: {success: true, category: category.to_data_hash} end rescue => ex Rails.logger.error ex. Rails.logger.error ex.backtrace.join("\n") # email error ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier render json: {success: false, message: 'Application Error'} end end |