aginx 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 39853dfe96ae563897bc13707716068e6fa82b89
4
+ data.tar.gz: b7ed5e7ac5a8fc3f922c17a2bf047f682ef2dabc
5
+ SHA512:
6
+ metadata.gz: fac77d240a942d73cac82b0527b6695c7d896ea95d724401b2e71396d7f5ae3e6b0a28f973cf71e380434fb408d0f08958f10ef4765f4605196017ca152cd1ad
7
+ data.tar.gz: c899dce488873afddc973a3486f86f016e1c4fb1028812ab9a561747b13b763139cd74ad8c215259840fdd96a75bd03032b6cc1d22c610a42de175c8a1a58ee0
data/.editorconfig ADDED
@@ -0,0 +1,37 @@
1
+ # EditorConfig is awesome: http://EditorConfig.org
2
+
3
+ # top-most EditorConfig file
4
+ root = true
5
+
6
+ # Unix-style newlines with a newline ending every file
7
+ [*]
8
+ end_of_line = lf
9
+ insert_final_newline = true
10
+ trim_trailing_whitespace = true
11
+
12
+ # Matches multiple files with brace expansion notation
13
+ # Set default charset
14
+ [*.{js,rb,erb}]
15
+ charset = utf-8
16
+
17
+ # ruby indentation
18
+ [*.rb]
19
+ indent_style = space
20
+ indent_size = 2
21
+
22
+ # Tab indentation (no size specified)
23
+ [Makefile]
24
+ indent_style = tab
25
+
26
+ # Indentation override for all JS under lib directory
27
+ [*.js]
28
+ indent_style = space
29
+ indent_size = 2
30
+
31
+ [*.md]
32
+ trim_trailing_whitespace = false
33
+
34
+ # Matches the exact files either package.json or .travis.yml
35
+ [.travis.yml]
36
+ indent_style = space
37
+ indent_size = 2
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ # See https://help.github.com/articles/ignoring-files for more about ignoring files.
2
+ #
3
+ # If you find yourself ignoring temporary files generated by your text editor
4
+ # or operating system, you probably want to add a global ignore instead:
5
+ # git config --global core.excludesfile '~/.gitignore_global'
6
+
7
+ # Ignore bundler config.
8
+ /.bundle
9
+
10
+
11
+ # Ignore all logfiles and tempfiles.
12
+ /log/*
13
+ /tmp/*
14
+ !/log/.keep
15
+ !/tmp/.keep
16
+
17
+ # Ignore Byebug command history file.
18
+ /.idea/
19
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in aginx.gemspec
4
+ gemspec
data/aginx.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "aginx/version"
6
+
7
+ Gem::Specification.new do |gem|
8
+ gem.name = "aginx"
9
+ gem.version = Aginx::VERSION
10
+ gem.authors = ["Agideo Team"]
11
+ gem.email = ["von@agideo.com"]
12
+ gem.description = "Scaffolds for Rails applications"
13
+ gem.summary = "Scaffolds for Rails applications"
14
+ gem.homepage = ""
15
+ gem.license = "MIT"
16
+
17
+ gem.files = `git ls-files`.split($/)
18
+ gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
19
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
20
+ gem.require_paths = ["lib"]
21
+
22
+ gem.add_dependency "activesupport", ">= 3.0.0"
23
+ end
data/lib/aginx.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "pundit/version"
2
+
3
+ module Aginx
4
+ module Generators; end
5
+ end
@@ -0,0 +1,15 @@
1
+ Description:
2
+ Stubs out a new api controller. Pass the controller name, either
3
+ CamelCased or under_scored, and a list of methods as arguments.
4
+
5
+ To create a controller within a module, specify the controller name as a
6
+ path like 'parent_module/controller_name'.
7
+
8
+ This generates a controller class in app/controllers
9
+
10
+ Example:
11
+ `rails generate aginx:controller Api/V1/CreditCards`
12
+ `rails generate aginx:controller api/v1/credit_cards`
13
+
14
+ This will create:
15
+ Controller: app/controllers/api/v1/credit_cards_controller.rb
@@ -0,0 +1,66 @@
1
+ module Aginx
2
+ module Generators
3
+ class ControllerGenerator < Rails::Generators::NamedBase
4
+ class_option :columns, type: :array, default: []
5
+ class_option :skip_model, type: :boolean, default: false,
6
+ desc: "Indicates when to skip generate columns from an named model, this will be true automatically when arguments is not empty"
7
+ class_option :skip_search, type: :boolean, default: false,
8
+ desc: "Indicates when to skip generate codes about search" # TODO --skip-search开关用于控制器是否支持搜索
9
+ class_option :skip_sort, type: :boolean, default: false,
10
+ desc: "Indicates when to skip generate codes about sort" # TODO --skip-sort开关用于控制器是否支持排序
11
+
12
+ source_root File.expand_path('../templates', __FILE__)
13
+
14
+ def init
15
+ prepare
16
+ end
17
+
18
+ def create_controller
19
+ template 'controller.rb', File.join('app/controllers', class_path, "#{@model_table_name}_controller.rb")
20
+ end
21
+
22
+ def create_route
23
+ routes_code = <<-EOF
24
+ resources :#{@model_table_name} do
25
+ collection do
26
+ delete 'multi_destroy'
27
+ end
28
+ end
29
+ EOF
30
+
31
+ # 兼容 agi_scaffold, 索引字符串前添加空格
32
+ inject_into_file 'config/routes.rb', before: " # Do not delete this line for agi_scaffold" do
33
+ routes_code
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def prepare
40
+ check_namespace
41
+ set_model
42
+ end
43
+
44
+ def check_namespace
45
+ raise 'Namespace is not specified!' if class_path.length == 0
46
+ end
47
+
48
+ def set_model
49
+ @model_table_name = file_name.underscore.pluralize
50
+ @model_name = file_name.camelize.singularize
51
+ @model_columns = options[:columns]
52
+ if @model_columns.length == 0 && !options[:skip_model]
53
+ model = @model_name.constantize
54
+ if model.respond_to? :columns
55
+ @model_columns = model.columns.map{|column| "#{column.name}:#{column.type}"}
56
+ else
57
+ raise NameError
58
+ end
59
+ end
60
+ rescue NameError
61
+ raise "Can't any table information about #{@model_name} model"
62
+ end
63
+
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,86 @@
1
+ <% var_name = @model_name.underscore -%>
2
+ <% table_name = @model_table_name -%>
3
+ <% pluralize_instance_name = "@" + table_name -%>
4
+ <% instance_name = "@" + var_name -%>
5
+ <% controller_namespace = class_path.map(&:capitalize).join("::") -%>
6
+ <% module_namespacing do -%>
7
+ module <%= controller_namespace %>
8
+ class <%= @model_name.pluralize %>Controller < ApiController
9
+ before_action :set_<%= var_name %>, only: [:show, :update, :destroy]
10
+
11
+ def index
12
+ search = OpenStruct.new params[:search]
13
+ sort = OpenStruct.new params[:sort]
14
+
15
+ <%= pluralize_instance_name %> = <%= @model_name %>.all
16
+ <%= pluralize_instance_name %> = build_search(<%= pluralize_instance_name %>, search)
17
+ <%= pluralize_instance_name %> = build_sort(<%= pluralize_instance_name %>, sort)
18
+
19
+ paginate json: <%= pluralize_instance_name %>, status: :ok, serializer: PaginationSerializer
20
+ end
21
+
22
+ def show
23
+ render json: <%= instance_name %>, status: :ok, serializer: ApiSerializer
24
+ end
25
+
26
+ def create
27
+ <%= instance_name %> = <%= @model_name %>.new(<%=var_name %>_params)
28
+
29
+ if <%= instance_name %>.save
30
+ render json: <%= instance_name %>, status: :created, location: [:v1, <%= instance_name %>], message: { type: 'success', cnt: I18n.t('flash.success', {action: I18n.t('buttons.create')}) || '创建成功' }, serializer: ApiSerializer
31
+ else
32
+ render json: <%= instance_name %>, serializer: ApiSerializer, status: :unprocessable_entity
33
+ end
34
+ end
35
+
36
+ def update
37
+ if <%= instance_name %>.update(<%=var_name %>_params)
38
+ render json: <%= instance_name %>, status: :ok, location: [:v1, <%= instance_name %>], message: { type: 'success', cnt: I18n.t('flash.success', {action: I18n.t('buttons.update')}) || '更新成功' }, serializer: ApiSerializer
39
+ else
40
+ render json: <%= instance_name %>, serializer: ApiSerializer, status: :unprocessable_entity
41
+ end
42
+ end
43
+
44
+ def destroy
45
+ if <%= instance_name %>.destroy
46
+ render json: <%= instance_name %>, status: :ok, message: { type: 'success', cnt: I18n.t('flash.success', {action: I18n.t('buttons.destroy')}) || '删除成功' }, serializer: ApiSerializer
47
+ else
48
+ render json: <%= instance_name %>, serializer: ApiSerializer, status: :unprocessable_entity
49
+ end
50
+ end
51
+
52
+ def multi_destroy
53
+ <%= pluralize_instance_name %> = <%= @model_name %>.where(id: params[:ids])
54
+ results = <%= pluralize_instance_name %>.destroy_all
55
+ render json: { success: true, ids: results.map(&:id), message: { type: 'success', cnt: I18n.t('flash.success', {action: I18n.t('buttons.multi_destroy')}) || '批量删除成功' }, status: :ok }
56
+ end
57
+
58
+ private
59
+
60
+ def set_<%=var_name %>
61
+ <%= instance_name %> = <%= @model_name %>.find(params[:id])
62
+ end
63
+
64
+ def <%=var_name %>_params
65
+ params.require(:<%=var_name %>).permit(<%= @model_columns.map{ |i| ":" + i.split(':').first }.join(", ") %>)
66
+ end
67
+
68
+ def build_search(records, search)
69
+ <% @model_columns.select {|i| ['string', 'date', 'integer'].include?(i.split(':').last)}.sort_by {|i| i.split(':').last}.map {|i| i.split(':').first}.each do |column_name| -%>
70
+ records = records.where(<%= column_name -%>: search.<%= column_name %>) if search.<%= column_name %>.present?
71
+ <% end -%>
72
+
73
+ if search.query.present?
74
+ cols = [<%= @model_columns.select {|i| ['boolean', 'float', 'decimal'].exclude?(i.split(':').last)}.map{|i| i.split(':').first}.map{|colname| "'`#{table_name}`.`#{colname}` LIKE :query'"}.join(", ") %>]
75
+ records = records.where(cols.join(" OR "), {query: "%#{search.query}%"})
76
+ end
77
+
78
+ records
79
+ end
80
+
81
+ def build_sort(records, sort)
82
+ records
83
+ end
84
+ end
85
+ end
86
+ <% end -%>
@@ -0,0 +1,14 @@
1
+ Description:
2
+ Generates locales for the given model name. If no argument is passed in,
3
+ this generator will generate locales for the all colums that model have,
4
+ and locales will be their columns name
5
+
6
+ Example:
7
+ de rails d aginx:locale attachment user_name:中文测试1 user_id:中文测试2 -l 中文
8
+
9
+ This will :
10
+ insert config/locales/activerecord.en.yml
11
+ insert config/locales/activerecord.en.yml
12
+ insert config/locales/admin.en.yml
13
+ insert config/locales/activerecord.zh-CN.yml
14
+ insert config/locales/activerecord.zh-CN.yml
@@ -0,0 +1,112 @@
1
+ module Aginx
2
+ module Generators
3
+ class LocaleGenerator < Rails::Generators::NamedBase
4
+ source_root File.expand_path('../templates', __FILE__)
5
+ class_option :model_locale, type: :string, aliases: '-l', required: true,
6
+ desc: "Indicates model name locale" # TODO --skip-sort开关用于控制器是否支持排序
7
+
8
+ def init
9
+ prepare
10
+ end
11
+
12
+ # 创建中文本地化
13
+ def create_en_locales
14
+ yml_code = <<-EOF
15
+ #{@model_name.underscore}: #{@model_name}
16
+ EOF
17
+
18
+ inject_into_file(
19
+ 'config/locales/activerecord.en.yml',
20
+ before: " # Do not delete this line for agi_scaffold model") {yml_code}
21
+
22
+ yml_code = " #{@model_name.underscore}:\n"
23
+ yml_code << @en_locales.map{|l| " #{l}: #{l.camelize}"}.join("\n")
24
+ yml_code << "\n"
25
+
26
+ inject_into_file(
27
+ 'config/locales/activerecord.en.yml',
28
+ before: " # Do not delete this line for agi_scaffold attributes") {yml_code}
29
+
30
+ yml_code = <<-EOF
31
+ #{@model_name.pluralize.underscore}:
32
+ index:
33
+ title: #{@model_name}
34
+ edit:
35
+ title: Edit #{@model_name}
36
+ new:
37
+ title: New #{@model_name}
38
+ create:
39
+ success: Create #{@model_name} success
40
+ EOF
41
+
42
+ inject_into_file(
43
+ 'config/locales/admin.en.yml',
44
+ before: " # Do not delete this line for agi_scaffold") {yml_code}
45
+ end
46
+
47
+ # 创建中文本地化
48
+ def create_zh_locales
49
+ yml_code = <<-EOF
50
+ #{@model_name.underscore}: #{options[:model_locale]}
51
+ EOF
52
+ inject_into_file(
53
+ 'config/locales/activerecord.zh-CN.yml',
54
+ before: " # Do not delete this line for agi_scaffold model") {yml_code}
55
+
56
+ yml_code = " #{@model_name.underscore}:\n"
57
+ yml_code << @zh_locales.map{|l| " #{l[0]}: #{l[1]}"}.join("\n")
58
+ yml_code << "\n"
59
+
60
+ inject_into_file(
61
+ 'config/locales/activerecord.zh-CN.yml',
62
+ before: " # Do not delete this line for agi_scaffold attributes") {yml_code}
63
+
64
+ yml_code = <<-EOF
65
+ #{@model_name.pluralize.underscore}:
66
+ index:
67
+ title: #{options[:model_locale]}
68
+ edit:
69
+ title: 编辑 #{options[:model_locale]}
70
+ new:
71
+ title: 新建 #{options[:model_locale]}
72
+ create:
73
+ success: 创建 #{options[:model_locale]} 成功
74
+ EOF
75
+
76
+ inject_into_file(
77
+ 'config/locales/admin.zh-CN.yml',
78
+ before: " # Do not delete this line for agi_scaffold") {yml_code}
79
+ end
80
+
81
+
82
+ private
83
+
84
+ def prepare
85
+ set_model
86
+ set_en_model_locale
87
+ set_zh_model_locale
88
+ end
89
+
90
+ def set_model
91
+ @model_table_name = file_name.underscore.pluralize
92
+ @model_name = file_name.camelize.singularize
93
+ @model_columns = @model_name.constantize.columns.map{|column| column.name}
94
+ rescue
95
+ raise "未找到#{@model_name}相关表结构信息"
96
+ end
97
+
98
+ def set_en_model_locale
99
+ @en_locales = args.empty? ? @model_columns : args.map do |arg|
100
+ arg.split(':')[0]
101
+ end
102
+ end
103
+
104
+ def set_zh_model_locale
105
+ @zh_locales = args.empty? ? @model_columns : args.map do |arg|
106
+ arg.split(':')
107
+ end
108
+ end
109
+
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,26 @@
1
+ Description
2
+ this is doing the same thing as `rails generate model` does, Take it easy, dude :)
3
+
4
+ Examples:
5
+ `rails generate aginx:model account`
6
+
7
+ For Active Record and TestUnit it creates:
8
+
9
+ Model: app/models/account.rb
10
+ Test: test/models/account_test.rb
11
+ Fixtures: test/fixtures/accounts.yml
12
+ Migration: db/migrate/XXX_create_accounts.rb
13
+
14
+ `rails generate aginx:model post title:string body:text published:boolean`
15
+
16
+ Creates a Post model with a string title, text body, and published flag.
17
+
18
+ `rails generate aginx:model admin/account`
19
+
20
+ For Active Record and TestUnit it creates:
21
+
22
+ Module: app/models/admin.rb
23
+ Model: app/models/admin/account.rb
24
+ Test: test/models/admin/account_test.rb
25
+ Fixtures: test/fixtures/admin/accounts.yml
26
+ Migration: db/migrate/XXX_create_admin_accounts.rb
@@ -0,0 +1,8 @@
1
+ module Aginx
2
+ module Generators
3
+ class ModelGenerator < Rails::Generators::NamedBase
4
+ source_root File.expand_path('../templates', __FILE__)
5
+ hook_for :orm
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,10 @@
1
+ Description:
2
+ Generates a serializer for the given resource. If no arguments is passed in,
3
+ this generator will serialize the all columns from a giving model.
4
+ On the contrary, I think you already know what will happen :)
5
+
6
+ Example:
7
+ rails generate aginx:serializer api/v1/user
8
+
9
+ This will create:
10
+ /serializers/api/v1/user_serializer.rb
@@ -0,0 +1,34 @@
1
+ module Aginx
2
+ module Generators
3
+ class SerializerGenerator < Rails::Generators::NamedBase
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ def create_serializer
7
+ prepare
8
+ template 'serializer.rb', File.join('app/serializers', class_path, "#{file_name}_serializer.rb")
9
+ end
10
+
11
+ private
12
+
13
+ def prepare
14
+ set_model
15
+ end
16
+
17
+ def set_model
18
+ @model_table_name = file_name.underscore.pluralize
19
+ @model_name = file_name.camelize.singularize
20
+ if args.length == 0
21
+ model = @model_name.constantize
22
+ if model.respond_to? :columns
23
+ @model_columns = model.columns.map{|column| "#{column.name}"}
24
+ else
25
+ raise "未找到#{model}相关表结构信息"
26
+ end
27
+ else
28
+ @model_columns = args.map{|arg| arg.split(':')[0]}
29
+ end
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,6 @@
1
+ <% controller_namespace = class_path.map(&:capitalize).join("::") -%>
2
+ module <%= controller_namespace %>
3
+ class <%= @model_name %>Serializer < ActiveModel::Serializer
4
+ attributes <%= @model_columns.map{ |i| ":#{i}"}.join(", ") %>
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ module Aginx
2
+ VERSION = "0.1.0".freeze
3
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aginx
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Agideo Team
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-01-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ description: Scaffolds for Rails applications
28
+ email:
29
+ - von@agideo.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".editorconfig"
35
+ - ".gitignore"
36
+ - Gemfile
37
+ - aginx.gemspec
38
+ - lib/aginx.rb
39
+ - lib/aginx/generators/aginx/controller/USAGE
40
+ - lib/aginx/generators/aginx/controller/controller_generator.rb
41
+ - lib/aginx/generators/aginx/controller/templates/controller.rb
42
+ - lib/aginx/generators/aginx/locale/USAGE
43
+ - lib/aginx/generators/aginx/locale/locale_generator.rb
44
+ - lib/aginx/generators/aginx/model/USAGE
45
+ - lib/aginx/generators/aginx/model/model_generator.rb
46
+ - lib/aginx/generators/aginx/serializer/USAGE
47
+ - lib/aginx/generators/aginx/serializer/serializer_generator.rb
48
+ - lib/aginx/generators/aginx/serializer/templates/serializer.rb
49
+ - lib/aginx/version.rb
50
+ homepage: ''
51
+ licenses:
52
+ - MIT
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 2.6.13
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Scaffolds for Rails applications
74
+ test_files: []