当前位置: 首页 > 工具软件 > Bullet-Ruby > 使用案例 >

[ruby on rails]一些常用的gems

柳钟展
2023-12-01

一些好的gem推荐:https://ruby-china.org/wiki/gems

1. 图形验证码

rucaptcha
使用方法:http://docs.qzy.camp/docs/使用gem-rucaptcha

2. 计算网页浏览量

impressionist: https://github.com/charlotte-ruby/impressionist

3. 分页

pagy: https://ddnexus.github.io/pagy/how-to
kaminari: https://github.com/kaminari/kaminari

kaminari设置中文显示:在en.yml中en后面添加

 views:
    pagination:
      first: "« 首页"
      last: "末页 »"
      previous: "« 上一页"
      next: "下一页 »"
      truncate: "..."

4. 富文本编辑器

ckeditor: https://github.com/galetahub/ckeditor

5. console美观适合人看

awesome_rails_console: https://github.com/ascendbruce/awesome_rails_console

6. 高效读取excel

creek:https://github.com/pythonicrubyist/creek
roo: https://github.com/roo-rb/roo

7. 导出excel、csv

spreadsheet_architect: https://github.com/westonganger/spreadsheet_architect

用法:
(1)在ApplicationRecord中加入 include SpreadsheetArchitect

class ApplicationRecord < ActiveRecord::Base
  include SpreadsheetArchitect
end

(2)在model中定义spreadsheet_columns方法,同时设置导出到excel中的格式

class Movie < ApplicationRecord
  
  def spreadsheet_columns
    [
      ['Id', :id],
      ['标题', :title],
      ['时间', :created_at]
    ]
  end
  
  SPREADSHEET_OPTIONS = {
    spreadsheet_columns: :spreadsheet_columns,
    header_style: {background_color: 'AAAAAA', color: 'FFFFFF', align: :center, font_name: 'Arial', font_size: 10, bold: true, italic: false, underline: false},
    row_style: {background_color: nil, color: '000000', align: :left, font_name: 'Arial', font_size: 10, bold: false, italic: false, underline: false},
    sheet_name: self.name,
    column_styles: [],
    range_styles: [],
    conditional_row_styles: [],
    merges: [],
    borders: [],
    column_types: [],
  }
end

(3)controller中使用

def index
    @movies = Movie.where.not("title = ?", '')
    respond_to do |format|
      format.html
      format.xlsx {render xlsx: @movies , filename:"movies-#{Time.now.to_s}"}
    end
end

(4)下载excel

 #在view中
 <%= link_to 'download',index_movies_path  %>

8. 表单

simple_form: https://github.com/plataformatec/simple_form

9. 图标

bootstrap: https://github.com/twbs/bootstrap-sass
fontawesome:http://www.fontawesome.com.cn/get-started/
gem ‘jquery-rails’
gem ‘bootstrap-sass’
gem ‘font-awesome-sass’

#application.scss

@import "bootstrap-sprockets";
@import 'bootstrap';
@import "font-awesome-sprockets";
@import "font-awesome";

#devise中让notice的样式继承alert-success,让alert样式继承alert-danger
.alert-notice{
  @extend .alert-success;
}
.alert-alert{
   @extend .alert-danger;
}
#application.js
//= require jquery
//= require bootstrap-sprockets
#提交表单时候有个转圈图标
<%= f.button "确认", data:{ disable_with: "<span class='fa fa-spinner fa-spin'></span>请稍等..." } %>

10. 自定义排列顺序acts_as_list

https://github.com/swanandp/acts_as_list

用法:在需要自定义排序的model添加position栏位

class AddPositionToEvents < ActiveRecord::Migration[5.0]
  def change
    add_column :events, :position, :integer
  end
    add_index :events, :position
    
    Event.order(:updated_at).each.with_index(1) do |event, index|
      event.update_column :position, index
    end
end

在model中加入acts_as_list

class Event < ApplicationRecord
	acts_as_list
end

routes中添加路由

resources :events do
	collection do
	    post :bulk_update
    end
    member do
        patch :move_up
        patch :move_down
    end
end

controller中添加move_up , move_down

def move_up
    @event = Event.find(params[:id])
    @event.move_higher
    flash[:success] = 'move up success'
    redirect_to  admin_events_path

  end

  def move_down
    @event = Event.find(params[:id])
    @event.move_lower
    flash[:success] = 'move down success'
    redirect_to admin_events_path

  end

view中添加按钮

 <%= link_to 'move Up', move_up_admin_event_path(event), method: :patch,class:'btn btn-default'  %>
 <%= link_to 'move Down', move_down_admin_event_path(event), method: :patch,class:'btn btn-default'  %>

11. 拖拉UI

gem ‘jquery-ui-rails’

12. 赞、喜欢、收藏、关注、订阅、屏蔽等 action-store

https://github.com/rails-engine/action-store

13. 用户通知 notifications

https://github.com/rails-engine/notifications

14. http抓包 rest-client 配合nokogiri使用

#rest-clent
https://github.com/rest-client/rest-client
#nokogiri
https://github.com/sparklemotion/nokogiri

15. 检查N+1查询 bullet

https://github.com/flyerhzm/bullet

用法:

gem 'bullet', group: 'development'
 rails g bullet:install
#config/environments/development.rb
 config.after_initialize do
    Bullet.enable = true
    Bullet.alert = true
 end

16. 代码质量分析 rubycritic

https://github.com/whitesmith/rubycritic

17. 代码质量风格分析 按ruby风格指南分析 rubocop

https://github.com/rubocop-hq/rubocop

18. pdf生成器 prawn

https://github.com/prawnpdf/prawn

19. 生成现有数据关系 rails-erd

https://github.com/voormedia/rails-erd

 类似资料: