shikigami 0.1.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +128 -0
- data/app/controllers/shikigami/base_controller.rb +79 -0
- data/lib/generators/shikigami/install_generator.rb +17 -0
- data/lib/generators/shikigami/templates/locale.yml +16 -0
- data/lib/shikigami.rb +6 -0
- data/lib/shikigami/bootstrap_button.rb +52 -0
- data/lib/shikigami/engine.rb +7 -0
- data/lib/shikigami/railtie.rb +10 -0
- data/lib/shikigami/version.rb +4 -0
- data/lib/shikigami/view_helpers.rb +35 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 946fda0f686e374b07bc61862dc2de9bfe897ee8
|
4
|
+
data.tar.gz: de9466a0dfc2350587f2ba11cb00a0c0564c8ee3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2453eefc4dd805710cc3403b650a6701d764497d1e60fcba164526e0050489fe83cc9c828cfebee4fa634156ce917b0b61781f63b1ef90d97ae484c139508305
|
7
|
+
data.tar.gz: a97e442433f3bf6edbb88498acbe9ffe3fd96652b49b12c03b2f6c6c2ea68e08e0e04f77fcbb2dcc34b12854f0bd2664de3b420f0c8060545377cfaacac0f0b0
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Tsehau Chao
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
# shikigami / 式神
|
2
|
+
[](https://badge.fury.io/rb/shikigami)
|
3
|
+
[](https://codeclimate.com/github/jodeci/shikigami)
|
4
|
+
[](https://codeclimate.com/github/jodeci/shikigami/coverage)
|
5
|
+
[](https://travis-ci.org/jodeci/shikigami)
|
6
|
+
|
7
|
+
*shikigami* is meant to make creating basic CRUD Rails apps as brain dead easy as possible.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```
|
14
|
+
gem "shikigami"
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
```
|
20
|
+
$ bundle install
|
21
|
+
```
|
22
|
+
|
23
|
+
*shikigami* utilizes the I18n library and you will need to have these information in your locale file:
|
24
|
+
|
25
|
+
```
|
26
|
+
zh-TW:
|
27
|
+
success:
|
28
|
+
create: 新建資料成功
|
29
|
+
update: 修改資料成功
|
30
|
+
destroy: 刪除資料成功
|
31
|
+
|
32
|
+
actions:
|
33
|
+
new: 新增
|
34
|
+
edit: 修改
|
35
|
+
show: 檢視
|
36
|
+
destroy: 刪除
|
37
|
+
|
38
|
+
confirm:
|
39
|
+
destroy: 確認要刪除?
|
40
|
+
|
41
|
+
warnings:
|
42
|
+
no_data: 尚無資料
|
43
|
+
```
|
44
|
+
|
45
|
+
Or, just let *shikigami* generate the locale file for you:
|
46
|
+
|
47
|
+
```
|
48
|
+
$ rails generate shikigami:install
|
49
|
+
```
|
50
|
+
|
51
|
+
*shikigami* also assumes that you use [Kaminari](https://github.com/amatsuda/kaminari) for pagination and bootstrap for layout.
|
52
|
+
|
53
|
+
Tested with Rails 5 and Ruby 2.3.
|
54
|
+
|
55
|
+
## Usage
|
56
|
+
|
57
|
+
*Original credit goes to [@ryudoawaru](https://github.com/ryudoawaru). I've been using this almost everywhere, with some tweaks of my own, so I figured I'd might as well put together a gem.*
|
58
|
+
|
59
|
+
*shikigami* provides an abstract controller to handle CRUD actions in `Shikigami::BaseController`. Simply inherit your controllers from *shikigami*, specify the relevant `current_scope` and `resource_params`, and you're done. *shikigami* will do the remaining boring stuff for you.
|
60
|
+
|
61
|
+
```
|
62
|
+
# all set!
|
63
|
+
class BooksController < Shikigami::BaseController
|
64
|
+
def current_scope
|
65
|
+
Book
|
66
|
+
end
|
67
|
+
|
68
|
+
def resource_params
|
69
|
+
parms.require(:book).permit(:title, :author)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
Then, in your view, just use `current_collection` or `current_object` to access your data.
|
75
|
+
|
76
|
+
```
|
77
|
+
# app/views/books/show.html.slim
|
78
|
+
h1 = current_object.title
|
79
|
+
|
80
|
+
# app/views/books/index.html.slim
|
81
|
+
- current_collection.each do |item|
|
82
|
+
= item.title
|
83
|
+
```
|
84
|
+
|
85
|
+
Obviously, *shikigami* is only meant for the most basic CRUD Rails apps. If you need something more fancy, you shouldn't be using it :p
|
86
|
+
|
87
|
+
## View Helpers
|
88
|
+
|
89
|
+
### no\_data\_alert
|
90
|
+
|
91
|
+
Generates the following HTML:
|
92
|
+
|
93
|
+
```
|
94
|
+
# no_data_alert
|
95
|
+
<div class="alert alert-warning">尚無資料</div>
|
96
|
+
```
|
97
|
+
|
98
|
+
### bootstrap\_dropdown\_toggle
|
99
|
+
|
100
|
+
Generates the following HTML. `bs_dd_toggle` for short. Yeah, I'm lazy.
|
101
|
+
|
102
|
+
```
|
103
|
+
# bootstrap_dropdown_toggle("something")
|
104
|
+
<a class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">something<span class="caret"></span></a>
|
105
|
+
```
|
106
|
+
|
107
|
+
### bootstrap\_button
|
108
|
+
|
109
|
+
Generates a bootstrap button style link. `bs_btn`
|
110
|
+
for short.
|
111
|
+
|
112
|
+
```
|
113
|
+
# bs_btn(:edit, "#")
|
114
|
+
<a class="btn btn-xs btn-warning" href="#">修改</a>
|
115
|
+
```
|
116
|
+
|
117
|
+
By default, *shikigami* generated buttons will be sized as `.btn-xs`. Unless specified otherwise, it will use `.btn-info` for `show` actions, `.btn-warning` for `edit` actions, and `.btn-danger` for `destroy` actions.
|
118
|
+
|
119
|
+
You can also pass in methods and confirmation:
|
120
|
+
|
121
|
+
```
|
122
|
+
# bs_btn(:destroy, "#", method: :delete, confirm: true)
|
123
|
+
<a class="btn btn-xs btn-danger" data-confirm="確認要刪除?" rel="nofollow" data-method="delete" href="#">刪除</a>
|
124
|
+
```
|
125
|
+
|
126
|
+
## License
|
127
|
+
|
128
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Shikigami
|
3
|
+
class BaseController < ApplicationController
|
4
|
+
helper_method :current_collection, :current_object
|
5
|
+
|
6
|
+
def index
|
7
|
+
end
|
8
|
+
|
9
|
+
def show
|
10
|
+
end
|
11
|
+
|
12
|
+
def new
|
13
|
+
@current_object = collection_scope.new
|
14
|
+
end
|
15
|
+
|
16
|
+
def create
|
17
|
+
@current_object = collection_scope.new(resource_params)
|
18
|
+
return render action: :new unless @current_object.save
|
19
|
+
action_success
|
20
|
+
end
|
21
|
+
|
22
|
+
def destroy
|
23
|
+
current_object.destroy
|
24
|
+
action_success
|
25
|
+
end
|
26
|
+
|
27
|
+
def edit
|
28
|
+
end
|
29
|
+
|
30
|
+
def update
|
31
|
+
if current_object.update(resource_params)
|
32
|
+
action_success
|
33
|
+
else
|
34
|
+
respond_to do |format|
|
35
|
+
format.html { return render action: :edit }
|
36
|
+
format.json
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def action_success
|
44
|
+
respond_to do |format|
|
45
|
+
format.html do
|
46
|
+
flash[:success] ||= t("success.#{action_name}")
|
47
|
+
redirect_to send("url_after_#{action_name}")
|
48
|
+
end
|
49
|
+
format.json
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# override in controller if needed
|
54
|
+
def url_after_create
|
55
|
+
url_for(action: :edit, id: current_object.id)
|
56
|
+
end
|
57
|
+
|
58
|
+
def url_after_update
|
59
|
+
url_for(action: :edit, id: current_object.id)
|
60
|
+
end
|
61
|
+
|
62
|
+
def url_after_destroy
|
63
|
+
url_for(action: :index)
|
64
|
+
end
|
65
|
+
|
66
|
+
# You should implement these in your controller
|
67
|
+
def collection_scope; end
|
68
|
+
|
69
|
+
def resource_params; end
|
70
|
+
|
71
|
+
def current_collection
|
72
|
+
@current_collection ||= collection_scope.page(params[:page])
|
73
|
+
end
|
74
|
+
|
75
|
+
def current_object
|
76
|
+
@current_object ||= collection_scope.find(params[:id])
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require "rails/generators/base"
|
3
|
+
module Shikigami
|
4
|
+
class InstallGenerator < Rails::Generators::Base
|
5
|
+
source_root File.expand_path("templates", __FILE__)
|
6
|
+
desc "Creates a locale file for your application"
|
7
|
+
class_option :locale, default: Rails.application.config.i18n.default_locale
|
8
|
+
|
9
|
+
def self.source_root
|
10
|
+
@source_root ||= File.join(File.dirname(__FILE__), "templates")
|
11
|
+
end
|
12
|
+
|
13
|
+
def copy_locale
|
14
|
+
copy_file "locale.yml", "config/locales/shikigami.#{options[:locale]}.yml"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/shikigami.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require "i18n"
|
3
|
+
require "active_support/core_ext/hash"
|
4
|
+
module Shikigami
|
5
|
+
class BootstrapButton
|
6
|
+
def initialize(action, options = {})
|
7
|
+
@action = action.to_s
|
8
|
+
@options = options
|
9
|
+
end
|
10
|
+
|
11
|
+
def options
|
12
|
+
{
|
13
|
+
class: default_class,
|
14
|
+
method: @options[:method],
|
15
|
+
data: confirm_message,
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def size
|
22
|
+
@options[:size] || "xs"
|
23
|
+
end
|
24
|
+
|
25
|
+
def style
|
26
|
+
@options[:style] || default_style
|
27
|
+
end
|
28
|
+
|
29
|
+
def confirm_message
|
30
|
+
{ confirm: default_message } if @options[:confirm]
|
31
|
+
end
|
32
|
+
|
33
|
+
def default_message
|
34
|
+
I18n.t("confirm.#{@action}", raise: true)
|
35
|
+
rescue
|
36
|
+
"Are you sure?"
|
37
|
+
end
|
38
|
+
|
39
|
+
def default_class
|
40
|
+
"btn btn-#{size} btn-#{style}"
|
41
|
+
end
|
42
|
+
|
43
|
+
def default_style
|
44
|
+
case @action
|
45
|
+
when "show" then "info"
|
46
|
+
when "edit" then "warning"
|
47
|
+
when "destroy" then "danger"
|
48
|
+
else "primary"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require "rails/railtie"
|
3
|
+
require "shikigami/view_helpers"
|
4
|
+
module Shikigami
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
initializer "shikigami.view_helpers" do
|
7
|
+
ActiveSupport.on_load(:action_view) { include Shikigami::ViewHelpers }
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require "shikigami/bootstrap_button"
|
3
|
+
module Shikigami
|
4
|
+
module ViewHelpers
|
5
|
+
def bootstrap_dropdown_toggle(title)
|
6
|
+
content_tag :a, bootstrap_dropdown_toggle_hash do
|
7
|
+
concat title
|
8
|
+
concat content_tag :span, nil, class: "caret"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
alias bs_dd_toggle bootstrap_dropdown_toggle
|
12
|
+
|
13
|
+
def bootstrap_button(action, path, options = {})
|
14
|
+
button = Shikigami::BootstrapButton.new(action, options)
|
15
|
+
link_to t("actions.#{action}"), path, button.options
|
16
|
+
end
|
17
|
+
alias bs_btn bootstrap_button
|
18
|
+
|
19
|
+
def no_data_alert(message = t("warnings.no_data"))
|
20
|
+
content_tag :div, message, class: "alert alert-warning"
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def bootstrap_dropdown_toggle_hash
|
26
|
+
{
|
27
|
+
"class": "dropdown-toggle",
|
28
|
+
"data-toggle": "dropdown",
|
29
|
+
"role": "button",
|
30
|
+
"aria-haspopup": "true",
|
31
|
+
"aria-expanded": "false",
|
32
|
+
}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: shikigami
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.11
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tsehau Chao
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-11-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: kaminari
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.0'
|
55
|
+
description: very early devlopment, use at own risk XD
|
56
|
+
email:
|
57
|
+
- jodeci@5xruby.tw
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- LICENSE.txt
|
63
|
+
- README.md
|
64
|
+
- app/controllers/shikigami/base_controller.rb
|
65
|
+
- lib/generators/shikigami/install_generator.rb
|
66
|
+
- lib/generators/shikigami/templates/locale.yml
|
67
|
+
- lib/shikigami.rb
|
68
|
+
- lib/shikigami/bootstrap_button.rb
|
69
|
+
- lib/shikigami/engine.rb
|
70
|
+
- lib/shikigami/railtie.rb
|
71
|
+
- lib/shikigami/version.rb
|
72
|
+
- lib/shikigami/view_helpers.rb
|
73
|
+
homepage: https://github.com/jodeci/shikigami
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.6.6
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: making CRUD apps as brain dead easy as possible
|
97
|
+
test_files: []
|