Trên GitHub, Hacker0x01/arel_toolkit đã đạt 16 sao, ngôn ngữ Ruby.
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
[1] > sql = 'SELECT id FROM users'
=> "SELECT id FROM users;"
[2] > arel = Arel.sql_to_arel(sql)
=> #<Arel::SelectManager:0x00007fe4e39823d8>
[3] > arel.to_sql
=> "SELECT \"id\" FROM \"users\""
Enhanced Arel AST
Arel.enhance(arel) adds additional information and helper methods to the existing Arel AST. This allows for mutating the AST, adding contextual information to the AST and querying for nodes. Some examples:
An Arel::Table is used in multiple different places inside the AST, and those locations will give the Arel::Table a different meaning. Used within a projection (column_reference) like SELECT posts.id has a different meaning than within a from SELECT * FROM posts (range_variable). The following example results in Arel::Table nodes where the object is used in the context of referencing a column:
Replace or remove nodes without modifying the original arel
remove and replace allow for modifications to the Arel AST. The changes are aplied to a new copy of the AST, making sure the original AST is not touched. The replace method accepts any Arel node or a precomputed enhanced node for improved performance.
enhanced_arel.child_at_path(['ast', 'cores', 0, 'projections', 1]).replace(Post.arel_table[:content])
enhanced_arel.child_at_path(['ast', 'cores', 0, 'projections', 0]).remove
enhanced_arel.to_sql
=> SELECT "posts"."content" FROM "posts" WHERE "posts"."id" = $1
Middleware
Creating Arel from SQL and enhancing Arel is just the beginning, where this gem really shines is the ability to modify Arel ASTs using middleware.
Middleware sits between ActiveRecord and the database, it allows you to alter the Arel (the SQL query) before it's send to the database. Multiple middlewares are supported by passing the results from a finished middleware to the next. Next to the arel object, a context object is used that acts as a intermediate storage between middlewares.
The middleware works out of the box in combination with Rails. If using ActiveRecord standalone you need to run the following after setting up the database connection:
Arel::Middleware::Railtie.insert
Example
Create middleware which can be any Ruby object as long as it responds to call. Middleware accepts 2 or 3 arguments, context is optional. Calling .call on next_middleware invokes the next middleware in the chain, returning the response from the database.
In this example, we're creating a middleware that will reorder any query. Next to reordering, we're adding an additional middleware that prints out the result of the reorder middleware.
class ReorderMiddleware
def self.call(arel, next_middleware)
enhanced_arel = Arel.enhance(arel)
enhanced_arel.query(class: Arel::Nodes::SelectStatement).each do |node|
arel_table = node.child_at_path(['cores', 0, 'source', 'left']).object
node['orders'].replace([arel_table[:id].asc])
end
new_arel = arel.order(Post.arel_table[:id].asc)
next_middleware.call(new_arel)
end
end
class LoggingMiddleware
def self.call(arel, next_middleware, context)
puts "User executing query: `#{context[:current_user_id]}`"
puts "Original SQL: `#{context[:original_sql]}`"
puts "Modified SQL: `#{arel.to_sql}`"
next_middleware.call(arel)
end
end
Now that we've defined our middelwares, it's time to see them in action:
[1] > Arel.middleware.apply([ReorderMiddleware, LoggingMiddleware]).context(current_user_id: 1) { Post.all.load }
User executing query: `1`
Original SQL: `SELECT "posts".* FROM "posts"`
Modified SQL: `SELECT "posts".* FROM "posts" ORDER BY "posts"."id" ASC`
Post Load (4.1ms) SELECT "posts".* FROM "posts" ORDER BY "posts"."id" ASC
=> []
This gem ships with a couple of middelware methods that allow you to fine-tune what and when to apply middelware.
This gem aims to have full support for PostgreSQL's SQL. In order to do so, it needs to add missing Arel nodes and extends the existing visitors. A full list of extensions on Arel can be found here: lib/arel/extensions.
Development
Check out the repo
Run bin/setup to install dependencies.
Start the postgres database docker compose up
Run bundle exec rspec to run the tests
You can also run bin/console for an interactive prompt that will allow you to experiment. To install this gem onto your local machine, run bundle exec rake install.
Releasing
Update version in version.rb
Create a new branch v<<VERSION_HERE>>
Run bundle install
Run bundle exec rake changelog
Commit the changes
Open a PR with name Version <<VERSION_HERE>> (example)
Merge the PR
Checkout the master branch and pull latest changes
Run bundle exec rake release
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/mvgijssel/arel_toolkit. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the ArelToolkit project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/Hacker0x01/arel_toolkit là nguồn chính thức.
Hacker0x01/arel_toolkit có bao nhiêu sao?
Hacker0x01/arel_toolkit có 16 sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/Hacker0x01/arel_toolkit. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
Hacker0x01/arel_toolkit có phải mã nguồn mở không?
Có — Hacker0x01/arel_toolkit phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/Hacker0x01/arel_toolkit.
Hacker0x01/arel_toolkit có tag gì không?
Bản đồng bộ chưa ghi nhận topic GitHub nào cho Hacker0x01/arel_toolkit. GitHub topics hiển thị ở thanh bên phải trang repo — đó là nơi đáng kiểm tra nhất.
Hacker0x01/arel_toolkit có website riêng không?
TopGit chưa ghi nhận URL trang chủ cho Hacker0x01/arel_toolkit. Phần README ở tab phía trên thường có link demo, hoặc xem mô tả GitHub của repo.
Hacker0x01/arel_toolkit còn đang phát triển không?
Commit gần nhất trên Hacker0x01/arel_toolkit là 1.3 năm trước (theo timestamp GitHub). Repo có 13 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
Đọc đầy đủ README ở tab phía trên.
Vẫn đang phân vân về arel_toolkit?
Một cú bấm sẽ gửi câu hỏi kèm trang này cho AI — xem AI nói gì về arel_toolkit.