首页
/ Periscope 技术文档

Periscope 技术文档

2024-12-26 03:08:57作者:裴锟轩Denise

本文档旨在帮助用户安装、使用和理解 Periscope 项目,以下为详细内容:

1. 安装指南

Periscope 依赖于以下 ORM 库,您需要根据所使用的 ORM 库选择相应的扩展库:

将相应的扩展库添加到您的项目依赖中,然后执行 bundle install 命令。

bundle add periscope-activerecord
bundle install

2. 项目使用说明

Periscope 提供了一种简单的方式来在您的模型上链式调用作用域(scopes),并允许用户公开访问这些作用域。

在您的控制器中,您可以使用 periscope 方法来自动根据请求参数构建作用域链。以下是一个示例:

class ArticlesController < ApplicationController
  def index
    @articles = Article.periscope(request.query_parameters)
  end
end

这样,您就可以通过查询参数来控制作用域的过滤条件。

3. 项目 API 使用文档

以下为 Periscope 的主要 API 方法:

  • scope_accessible: 在模型中指定哪些作用域可以公开访问。
  • periscope: 在控制器中使用该方法根据请求参数构建作用域链。

scope_accessible

在模型中,您可以使用 scope_accessible 方法指定哪些作用域可以被 Periscope 使用。以下示例中,gender 作用域被指定为可公开访问:

class User < ActiveRecord::Base
  scope :gender, proc { |g| where(gender: g) }
  scope :makes, proc { |s| where("salary >= ?", s) }

  scope_accessible :gender
end

periscope

在控制器中,您可以使用 periscope 方法根据请求参数自动构建作用域链:

class UsersController < ApplicationController
  def index
    @users = User.periscope(request.query_parameters)
  end
end

参数解析器

有时您需要对查询参数进行自定义解析,此时您可以为 scope_accessible 方法提供一个 :parser 选项:

class User < ActiveRecord::Base
  scope :gender, proc { |g| where(gender: g) }

  scope_accessible :gender, parser: proc { |g| [g.downcase] }
end

开关作用域

对于不需要参数的作用域,您可以使用 :boolean => true 选项来创建一个开关作用域:

class User < ActiveRecord::Base
  scope :male, proc { where(gender: "male") }
  scope :female, proc { where(gender: "female") }

  scope_accessible :male, :female, boolean: true
end

自定义方法名

如果查询参数与现有方法名或 Ruby 保留字冲突,您可以使用 :method 选项指定不同的方法名:

class Project < ActiveRecord::Base
  scope_accessible :begin, method: :begins_after
  scope_accessible :end, method: :ends_before

  def self.begins_after(date)
    where("begins_at >= ?", date)
  end

  def self.ends_before(date)
    where("ends_at <= ?", date)
  end
end

或者,您可以使用 :prefix 和/或 :suffix 选项来自动生成方法名:

class Project < ActiveRecord::Base
  scope_accessible :begin, :end, suffix: "_date"

  def self.begin_date(date)
    where("begins_at >= ?", date)
  end

  def self.end_date(date)
    where("ends_at <= ?", date)
  end
end

4. 项目安装方式

请参考上述的安装指南,根据您的 ORM 库选择相应的扩展库并添加到您的项目依赖中。

登录后查看全文
热门项目推荐

项目优选

收起
kernelkernel
deepin linux kernel
C
22
6
docsdocs
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
162
2.05 K
nop-entropynop-entropy
Nop Platform 2.0是基于可逆计算理论实现的采用面向语言编程范式的新一代低代码开发平台,包含基于全新原理从零开始研发的GraphQL引擎、ORM引擎、工作流引擎、报表引擎、规则引擎、批处理引引擎等完整设计。nop-entropy是它的后端部分,采用java语言实现,可选择集成Spring框架或者Quarkus框架。中小企业可以免费商用
Java
8
0
ShopXO开源商城ShopXO开源商城
🔥🔥🔥ShopXO企业级免费开源商城系统,可视化DIY拖拽装修、包含PC、H5、多端小程序(微信+支付宝+百度+头条&抖音+QQ+快手)、APP、多仓库、多商户、多门店、IM客服、进销存,遵循MIT开源协议发布、基于ThinkPHP8框架研发
JavaScript
96
15
ohos_react_nativeohos_react_native
React Native鸿蒙化仓库
C++
198
279
leetcodeleetcode
🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解
Java
60
16
Git4ResearchGit4Research
Git4Research旨在构建一个开放、包容、协作的研究社区,让更多人能够参与到科学研究中,共同推动知识的进步。
HTML
22
1
apintoapinto
基于golang开发的网关。具有各种插件,可以自行扩展,即插即用。此外,它可以快速帮助企业管理API服务,提高API服务的稳定性和安全性。
Go
22
0
RuoYi-Vue3RuoYi-Vue3
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
950
557
risc-v64-naruto-pirisc-v64-naruto-pi
基于QEMU构建的RISC-V64 SOC,支持Linux,baremetal, RTOS等,适合用来学习Linux,后续还会添加大量的controller,实现无需实体开发板,即可学习Linux和RISC-V架构
C
19
5