Class: Api::V1::CapabilitiesController
- Inherits:
-
BaseController
- Object
- BaseController
- Api::V1::CapabilitiesController
- Defined in:
- app/controllers/api/v1/capabilities_controller.rb
Instance Method Summary collapse
- #add ⇒ Object
- #available ⇒ Object
- #index ⇒ Object
- #page ⇒ Object
- #per_page ⇒ Object
- #remove ⇒ Object
- #selected ⇒ Object
Instance Method Details
#add ⇒ Object
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 |
# File 'app/controllers/api/v1/capabilities_controller.rb', line 88 def add begin type = params[:type] id = params[:id] capability_ids = JSON.parse(params[:capability_ids]) assign_to = type.constantize.find(id) capability_ids.each do |capability_id| capability = Capability.find(capability_id) case type when 'User' assign_to.add_capability(capability) when 'SecurityRole' assign_to.add_capability(capability) when 'Group' assign_to.add_capability(capability) end end render :json => {:success => true, :message => 'Capability(s) Added'} rescue Exception => e Rails.logger.error e. Rails.logger.error e.backtrace.join("\n") render :inline => { :success => false, :message => e. }.to_json end end |
#available ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'app/controllers/api/v1/capabilities_controller.rb', line 54 def available type = params[:type] id = params[:id] sort = (params[:sort] || 'description').downcase sort = 'capabilities.description' if sort == 'description' dir = (params[:dir] || 'asc').downcase query_filter = params[:query_filter].strip rescue nil scope_type_ids = [ScopeType.find_by_internal_identifier('class').id, ScopeType.find_by_internal_identifier('query').id] statement = id.blank? ? Capability.joins(:capability_type) : type.constantize.find(id).capabilities_not.where("scope_type_id IN (#{scope_type_ids.join(',')})") statement = (params[:query_filter].blank? ? statement : statement.where("(UPPER(capabilities.description) LIKE UPPER('%#{query_filter}%'))")) available = statement.paginate(:page => page, :per_page => per_page, :order => "#{sort} #{dir}") render :json => {:total_count => statement.count, :capabilities => available.map { |capability| capability.to_data_hash }} 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 |
# File 'app/controllers/api/v1/capabilities_controller.rb', line 5 def index if params[:user_id].present? capabilities = User.find(params[:user_id]).capabilities else capabilities = Capability end respond_to do |format| format.json do query = params[:query] 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] start = params[:start] if query capability_role_tbl = Capability.arel_table capabilities = capabilities.where(capability_role_tbl[:description].matches("%#{query}%")) total_count = capabilities.count capabilities = capabilities.order("#{sort} #{dir}") else total_count = capabilities.count capabilities = capabilities.order("#{sort} #{dir}") end if limit and start capabilities = capabilities.limit(limit).offset(start) end render json: {success: true, total_count: total_count, capabilities: capabilities.collect{|capability| capability.to_data_hash}} end format.tree do nodes = [].tap do |nodes| capabilities.all.each do |capability| nodes.push({ leaf: true, internal_identifier: capability.id, text: capability.description }) end end render json: {success: true, capabilities: nodes} end end end |
#page ⇒ Object
149 150 151 152 |
# File 'app/controllers/api/v1/capabilities_controller.rb', line 149 def page offset = params[:start].to_f offset > 0 ? (offset / params[:limit].to_f).to_i + 1 : 1 end |
#per_page ⇒ Object
154 155 156 |
# File 'app/controllers/api/v1/capabilities_controller.rb', line 154 def per_page params[:limit].nil? ? 10 : params[:limit].to_i end |
#remove ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'app/controllers/api/v1/capabilities_controller.rb', line 118 def remove begin type = params[:type] id = params[:id] capability_ids = JSON.parse(params[:capability_ids]) assign_to = type.constantize.find(id) capability_ids.each do |capability_id| capability = Capability.find(capability_id) case type when 'User' assign_to.remove_capability(capability) when 'SecurityRole' assign_to.remove_capability(capability) when 'Group' assign_to.remove_capability(capability) end end render :json => {:success => true, :message => 'Capability(s) Removed'} rescue Exception => e Rails.logger.error e. Rails.logger.error e.backtrace.join("\n") render :inline => { :success => false, :message => e. }.to_json end end |
#selected ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'app/controllers/api/v1/capabilities_controller.rb', line 71 def selected type = params[:type] id = params[:id] sort = (params[:sort] || 'description').downcase sort = 'capabilities.description' if sort == 'description' dir = (params[:dir] || 'asc').downcase query_filter = params[:query_filter].strip rescue nil scope_type_ids = [ScopeType.find_by_internal_identifier('class').id, ScopeType.find_by_internal_identifier('query').id] statement = id.blank? ? Capability.joins(:capability_type) : type.constantize.find(id).capabilities.where("scope_type_id IN (#{scope_type_ids.join(',')})") statement = (params[:query_filter].blank? ? statement : statement.where("(UPPER(capabilities.description) LIKE UPPER('%#{query_filter}%'))")) selected = statement.paginate(:page => page, :per_page => per_page, :order => "#{sort} #{dir}") render :json => {:total_count => statement.count, :capabilities => selected.map { |capability| capability.to_data_hash }} end |