Class: Rhino::ResourcesController
- Inherits:
-
ActionController::API
- Object
- ActionController::API
- Rhino::ResourcesController
- Includes:
- Pundit::Authorization
- Defined in:
- lib/rhino/controllers/resources_controller.rb
Overview
Global CRUD controller that handles all registered models. Mirrors the Laravel GlobalController exactly.
Routes pass the model slug via route defaults, and this controller resolves the appropriate ActiveRecord class to operate on.
Constant Summary collapse
- @@organization_path_cache =
Cache for auto-detected organization paths (class-level, survives across requests)
{}
Instance Method Summary collapse
-
#computed ⇒ Object
GET /api/slug/computed?attributes=a,b.
-
#destroy ⇒ Object
DELETE /api/slug/:id.
-
#force_delete ⇒ Object
DELETE /api/slug/:id/force-delete.
-
#index ⇒ Object
GET /api/slug.
-
#nested ⇒ Object
POST /api/nested.
-
#restore ⇒ Object
POST /api/slug/:id/restore.
-
#show ⇒ Object
GET /api/slug/:id.
-
#store ⇒ Object
POST /api/slug.
-
#trashed ⇒ Object
GET /api/slug/trashed.
-
#update ⇒ Object
PUT /api/slug/:id.
Instance Method Details
#computed ⇒ Object
GET /api/slug/computed?attributes=a,b
Collection-level computed attributes: each declared callable is evaluated
ONCE over the whole (scoped + filtered) relation instead of once per row,
which is what makes aggregates such as active_users_count cheap.
The relation handed to each callable has the organization scope, the
model's default scopes, ?scope=, ?filter[]= and ?search= already
applied — so the numbers describe exactly the set index would have
listed. Sorting, sparse fieldsets, includes and pagination are
deliberately NOT applied.
Omitting ?attributes= returns every declared attribute the policy allows.
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/rhino/controllers/resources_controller.rb', line 216 def computed model_class, :index?, policy_class: policy_for(model_class) declared = collection_computed_attributes names = resolve_requested_collection_attributes(declared) return if performed? builder = QueryBuilder.new(model_class, params: params, named_scopes: true) apply_organization_scope(builder) builder.build_for_computed scope = builder.to_scope user = current_user data = names.each_with_object({}) do |name, memo| # Each attribute gets the base relation; ActiveRecord relations are # immutable under chaining, so one callable's constraints can never # leak into the next one's result. memo[name] = call_computed_attribute(declared[name], scope, user) end render json: { data: data } end |
#destroy ⇒ Object
DELETE /api/slug/:id
163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/rhino/controllers/resources_controller.rb', line 163 def destroy record = find_record record, :destroy?, policy_class: policy_for(record) if record.respond_to?(:discard!) record.discard! else record.destroy! end head :no_content end |
#force_delete ⇒ Object
DELETE /api/slug/:id/force-delete
252 253 254 255 256 257 258 259 |
# File 'lib/rhino/controllers/resources_controller.rb', line 252 def force_delete record = find_by_route_key(organization_scoped(model_class.discarded)) record, :force_delete?, policy_class: policy_for(record) record.destroy! head :no_content end |
#index ⇒ Object
GET /api/slug
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/rhino/controllers/resources_controller.rb', line 42 def index model_class, :index?, policy_class: policy_for(model_class) computed = resolve_requested_computed_attributes return if performed? builder = QueryBuilder.new(model_class, params: params, named_scopes: true) apply_organization_scope(builder) builder.build per_page = params[:per_page] pagination_enabled = model_class.try(:pagination_enabled) || false if per_page.present? || pagination_enabled result = builder.paginate set_pagination_headers(result[:pagination]) render json: { data: serialize_collection(result[:items], computed) } else render json: { data: serialize_collection(builder.to_scope, computed) } end end |
#nested ⇒ Object
POST /api/nested
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/rhino/controllers/resources_controller.rb', line 266 def nested operations = validate_nested_structure return if performed? nested_config = Rhino.config.nested max_ops = nested_config[:max_operations] if max_ops && operations.length > max_ops return render json: { message: "Too many operations.", errors: { operations: ["Maximum #{max_ops} operations allowed."] } }, status: :unprocessable_entity end allowed_models = nested_config[:allowed_models] if allowed_models.is_a?(Array) operations.each_with_index do |op, index| unless allowed_models.include?(op["model"]) return render json: { message: "Operation not allowed.", errors: { "operations.#{index}.model" => ["Model \"#{op['model']}\" is not allowed for nested operations."] } }, status: :unprocessable_entity end end end # Validate and authorize each operation validated_per_op = [] auth_results = [] operations.each_with_index do |operation, index| validated = validate_nested_operation(operation, index) return if performed? validated_per_op << validated auth_result = (operation, validated, index) return if performed? auth_results << auth_result end # Execute all operations in a transaction results = execute_nested_operations(operations, validated_per_op, auth_results) render json: { results: results } end |
#restore ⇒ Object
POST /api/slug/:id/restore
241 242 243 244 245 246 247 248 249 |
# File 'lib/rhino/controllers/resources_controller.rb', line 241 def restore record = find_by_route_key(organization_scoped(model_class.discarded)) record, :restore?, policy_class: policy_for(record) record.undiscard! record.reload render json: serialize_record(record) end |
#show ⇒ Object
GET /api/slug/:id
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/rhino/controllers/resources_controller.rb', line 100 def show record = find_record record, :show?, policy_class: policy_for(record) computed = resolve_requested_computed_attributes return if performed? # Apply includes if requested if params[:include].present? auth_response = return auth_response if auth_response builder = QueryBuilder.new(model_class, params: params) # record is already resolved via the route key; re-query by primary key builder.instance_variable_set(:@scope, model_class.where(model_class.primary_key => record.id)) apply_organization_scope(builder) builder.build record = builder.to_scope.first! end render json: serialize_record(record, computed) end |
#store ⇒ Object
POST /api/slug
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/rhino/controllers/resources_controller.rb', line 65 def store model_class, :create?, policy_class: policy_for(model_class) data = params_hash # Strip organization_id — it's auto-set by the framework data.delete("organization_id") if current_organization permitted_fields = resolve_permitted_fields(current_user, "create") # Check for forbidden fields → 403 forbidden = find_forbidden_fields(data, permitted_fields) if forbidden.any? return render json: { message: "You are not allowed to set the following field(s): #{forbidden.join(', ')}" }, status: :forbidden end model_instance = model_class.new validation = model_instance.validate_for_action( data, permitted_fields: permitted_fields, organization: current_organization ) unless validation[:valid] return render json: { errors: validation[:errors] }, status: :unprocessable_entity end validated = validation[:validated] add_organization_to_data(validated) record = model_class.create!(validated) render json: serialize_record(record), status: :created end |
#trashed ⇒ Object
GET /api/slug/trashed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/rhino/controllers/resources_controller.rb', line 181 def trashed model_class, :view_trashed?, policy_class: policy_for(model_class) computed = resolve_requested_computed_attributes return if performed? builder = QueryBuilder.new(model_class.discarded, params: params, named_scopes: true) apply_organization_scope(builder) builder.build per_page = params[:per_page] pagination_enabled = model_class.try(:pagination_enabled) || false if per_page.present? || pagination_enabled result = builder.paginate set_pagination_headers(result[:pagination]) render json: { data: serialize_collection(result[:items], computed) } else render json: { data: serialize_collection(builder.to_scope, computed) } end end |
#update ⇒ Object
PUT /api/slug/:id
124 125 126 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 154 155 156 157 158 159 160 |
# File 'lib/rhino/controllers/resources_controller.rb', line 124 def update record = find_record record, :update?, policy_class: policy_for(record) data = params_hash # Reject organization_id changes — cross-tenant reassignment is not allowed if current_organization && data.key?("organization_id") return render json: { message: "You are not allowed to change the organization_id." }, status: :forbidden end permitted_fields = resolve_permitted_fields(current_user, "update") # Check for forbidden fields → 403 forbidden = find_forbidden_fields(data, permitted_fields) if forbidden.any? return render json: { message: "You are not allowed to set the following field(s): #{forbidden.join(', ')}" }, status: :forbidden end model_instance = model_class.new validation = model_instance.validate_for_action( data, permitted_fields: permitted_fields, organization: current_organization ) unless validation[:valid] return render json: { errors: validation[:errors] }, status: :unprocessable_entity end record.update!(validation[:validated]) record.reload render json: serialize_record(record) end |