xavius-controllers 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/generators/xavius/action/USAGE +8 -0
- data/lib/generators/xavius/action/action_generator.rb +10 -0
- data/lib/generators/xavius/action/templates/action.rb +5 -0
- data/lib/xavius/actions/base.rb +14 -0
- data/lib/xavius/actions/context.rb +17 -0
- data/lib/xavius/actions/create.rb +9 -0
- data/lib/xavius/actions/destroy.rb +9 -0
- data/lib/xavius/actions/index.rb +9 -0
- data/lib/xavius/actions/update.rb +9 -0
- data/lib/xavius/controllers/base.rb +63 -0
- data/lib/xavius/controllers/helpers.rb +73 -0
- data/lib/xavius/controllers/version.rb +1 -1
- metadata +16 -15
- data/.gitignore +0 -11
- data/.rspec +0 -3
- data/.travis.yml +0 -6
- data/Gemfile +0 -4
- data/README.md +0 -36
- data/Rakefile +0 -6
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/xavius-controllers.gemspec +0 -33
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6e66917d0dc5a3438ba52d547fccda380b3267162686ed5d0232f459e34ea6e2
|
4
|
+
data.tar.gz: b3de3703f0892d241ea3423ea3dfa92e164b22404655ac2adec3e3015e457d48
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 34656a9e906407423635ea7c7f7e635a476114dd86819dc5cc326768a3da9dd81d0128a8599e2ca507fa4e80455786a57a8197a02283b6c2436ec2c187af6c90
|
7
|
+
data.tar.gz: 302f0ca80e1428e333bfb87cb05e9fe9adb61541adcf24490b4d727a47b288a02171f66b4e1de1956bd057fa93e5eb050012fc2e52941538abb967fe8462c00d
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
class Xavius::ActionGenerator < Rails::Generators::NamedBase
|
3
|
+
source_root File.expand_path('templates', __dir__)
|
4
|
+
desc 'This generator creates Action classes for you'
|
5
|
+
|
6
|
+
def create_action_class
|
7
|
+
@action_name, @namespace = ARGV
|
8
|
+
template "action.rb", "app/actions/#{@namespace}/#{@action_name}_action.rb"
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Xavius
|
2
|
+
module Actions
|
3
|
+
class Context
|
4
|
+
attr_reader :resource_class, :resource
|
5
|
+
|
6
|
+
def initialize(**params)
|
7
|
+
@resource = params.fetch(:resource)
|
8
|
+
@resource_class = @resource.class
|
9
|
+
@resource_params = params.fetch(:resource_params)
|
10
|
+
end
|
11
|
+
|
12
|
+
def resource_params
|
13
|
+
@resource_params.call
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module Xavius
|
4
|
+
module Controllers
|
5
|
+
module Base
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
include Xavius::Controllers::Helpers
|
10
|
+
before_action :set_resource
|
11
|
+
end
|
12
|
+
|
13
|
+
def index
|
14
|
+
instance_variable_set(instance_variable_name_for(collection_name), perform_action)
|
15
|
+
end
|
16
|
+
|
17
|
+
def create
|
18
|
+
respond_to do |format|
|
19
|
+
if perform_action
|
20
|
+
format.html { redirect_to action_context.resource, notice: success_message }
|
21
|
+
format.json { render :show, status: :created, location: action_context.resource }
|
22
|
+
else
|
23
|
+
format.html { render :new }
|
24
|
+
format.json { render json: action_context.resource.errors, status: :unprocessable_entity }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def update
|
30
|
+
respond_to do |format|
|
31
|
+
if perform_action
|
32
|
+
format.html { redirect_to action_context.resource, notice: success_message }
|
33
|
+
format.json { render :show, status: :ok, location: action_context.resource }
|
34
|
+
else
|
35
|
+
format.html { render :edit }
|
36
|
+
format.json { render json: action_context.resource.errors, status: :unprocessable_entity }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def destroy
|
42
|
+
perform_action
|
43
|
+
respond_to do |format|
|
44
|
+
format.html { redirect_to url_for(resource_class), notice: success_message }
|
45
|
+
format.json { head :no_content }
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
def perform_action
|
51
|
+
action_context.perform
|
52
|
+
end
|
53
|
+
|
54
|
+
def action_context
|
55
|
+
@action_context ||= action_class.new(context)
|
56
|
+
end
|
57
|
+
|
58
|
+
def context
|
59
|
+
Xavius::Actions::Context.new(resource: resource, resource_params: -> { resource_params })
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module Xavius
|
2
|
+
module Controllers
|
3
|
+
module Helpers
|
4
|
+
protected
|
5
|
+
|
6
|
+
def resource
|
7
|
+
instance_variable_get(instance_variable_name_for(instance_name))
|
8
|
+
end
|
9
|
+
|
10
|
+
def find_resource
|
11
|
+
if params[:id]
|
12
|
+
resource_class.find(params[:id])
|
13
|
+
else
|
14
|
+
resource_class.new
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def resource_class
|
19
|
+
class_name.constantize
|
20
|
+
end
|
21
|
+
|
22
|
+
def resource_human_name
|
23
|
+
resource_class.model_name.human
|
24
|
+
end
|
25
|
+
|
26
|
+
def set_resource
|
27
|
+
instance_variable_set(instance_variable_name_for(instance_name), find_resource)
|
28
|
+
end
|
29
|
+
|
30
|
+
def success_message
|
31
|
+
I18n.t('.success', default: I18n.t(:success, scope: [:resource, action_name], resource_name: resource_human_name))
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
def action_class
|
36
|
+
infer_user_action_class || "Xavius::Actions::#{action_name.camelize}".constantize
|
37
|
+
end
|
38
|
+
|
39
|
+
def infer_user_action_class
|
40
|
+
"#{module_parent}::#{controller_name.camelize.demodulize}::#{action_name.camelize}Action".safe_constantize ||
|
41
|
+
"#{controller_name.camelize}::#{action_name.camelize}Action".safe_constantize
|
42
|
+
end
|
43
|
+
|
44
|
+
def module_parent
|
45
|
+
if Rails::VERSION::MAJOR > 5
|
46
|
+
resource_class.module_parent
|
47
|
+
else
|
48
|
+
resource_class.parent
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def instance_variable_name_for(name)
|
53
|
+
"@#{name}"
|
54
|
+
end
|
55
|
+
|
56
|
+
def class_name
|
57
|
+
controller_name.singularize.camelize.freeze
|
58
|
+
end
|
59
|
+
|
60
|
+
def collection_name
|
61
|
+
infer_from_controller.freeze
|
62
|
+
end
|
63
|
+
|
64
|
+
def instance_name
|
65
|
+
infer_from_controller.singularize.freeze
|
66
|
+
end
|
67
|
+
|
68
|
+
def infer_from_controller
|
69
|
+
controller_name.gsub("/", "_")
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xavius-controllers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Emerson Xavier
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
9
|
+
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-06-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -67,29 +67,30 @@ dependencies:
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '5.0'
|
69
69
|
description: Keep your controllers clean and reuse common REST behaviour
|
70
|
-
email:
|
70
|
+
email:
|
71
71
|
executables: []
|
72
72
|
extensions: []
|
73
73
|
extra_rdoc_files: []
|
74
74
|
files:
|
75
|
-
-
|
76
|
-
-
|
77
|
-
-
|
78
|
-
-
|
79
|
-
-
|
80
|
-
-
|
81
|
-
-
|
82
|
-
-
|
75
|
+
- lib/generators/xavius/action/USAGE
|
76
|
+
- lib/generators/xavius/action/action_generator.rb
|
77
|
+
- lib/generators/xavius/action/templates/action.rb
|
78
|
+
- lib/xavius/actions/base.rb
|
79
|
+
- lib/xavius/actions/context.rb
|
80
|
+
- lib/xavius/actions/create.rb
|
81
|
+
- lib/xavius/actions/destroy.rb
|
82
|
+
- lib/xavius/actions/index.rb
|
83
|
+
- lib/xavius/actions/update.rb
|
83
84
|
- lib/xavius/controllers.rb
|
85
|
+
- lib/xavius/controllers/base.rb
|
86
|
+
- lib/xavius/controllers/helpers.rb
|
84
87
|
- lib/xavius/controllers/version.rb
|
85
|
-
- xavius-controllers.gemspec
|
86
88
|
homepage: https://github.com/xavius-rb/xavius/tree/master/xavius-controllers
|
87
89
|
licenses:
|
88
90
|
- MIT
|
89
91
|
metadata:
|
90
|
-
allowed_push_host: https://rubygems.org/
|
91
|
-
homepage_uri: https://github.com/xavius-rb/xavius/tree/master/xavius-controllers
|
92
92
|
source_code_uri: https://github.com/xavius-rb/xavius/tree/master/xavius-controllers
|
93
|
+
allowed_push_host: https://rubygems.org/
|
93
94
|
changelog_uri: https://github.com/xavius-rb/xavius/tree/master/xavius-controllers/CHANGELOG.md
|
94
95
|
post_install_message:
|
95
96
|
rdoc_options: []
|
data/.gitignore
DELETED
data/.rspec
DELETED
data/.travis.yml
DELETED
data/Gemfile
DELETED
data/README.md
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
# Xavius::Controllers
|
2
|
-
|
3
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/xavius/controllers`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
6
|
-
|
7
|
-
## Installation
|
8
|
-
|
9
|
-
Add this line to your application's Gemfile:
|
10
|
-
|
11
|
-
```ruby
|
12
|
-
gem 'xavius-controllers'
|
13
|
-
```
|
14
|
-
|
15
|
-
And then execute:
|
16
|
-
|
17
|
-
$ bundle install
|
18
|
-
|
19
|
-
Or install it yourself as:
|
20
|
-
|
21
|
-
$ gem install xavius-controllers
|
22
|
-
|
23
|
-
## Usage
|
24
|
-
|
25
|
-
TODO: Write usage instructions here
|
26
|
-
|
27
|
-
## Development
|
28
|
-
|
29
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
-
|
31
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
-
|
33
|
-
## Contributing
|
34
|
-
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/xavius-controllers.
|
36
|
-
|
data/Rakefile
DELETED
data/bin/console
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require "bundler/setup"
|
4
|
-
require "xavius/controllers"
|
5
|
-
|
6
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
-
# with your gem easier. You can also use a different console, if you like.
|
8
|
-
|
9
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
# require "pry"
|
11
|
-
# Pry.start
|
12
|
-
|
13
|
-
require "irb"
|
14
|
-
IRB.start(__FILE__)
|
data/bin/setup
DELETED
data/xavius-controllers.gemspec
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
require_relative 'lib/xavius/controllers/version'
|
2
|
-
|
3
|
-
Gem::Specification.new do |spec|
|
4
|
-
spec.name = "xavius-controllers"
|
5
|
-
spec.version = Xavius::Controllers::VERSION
|
6
|
-
spec.authors = "Emerson Xavier"
|
7
|
-
spec.email = "emersonx.engineer@gmail.com"
|
8
|
-
spec.summary = %q{Reusable controller utilities}
|
9
|
-
spec.description = %q{Keep your controllers clean and reuse common REST behaviour}
|
10
|
-
spec.homepage = "https://github.com/xavius-rb/xavius/tree/master/xavius-controllers"
|
11
|
-
spec.license = "MIT"
|
12
|
-
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
13
|
-
|
14
|
-
spec.metadata["allowed_push_host"] = "https://rubygems.org/"
|
15
|
-
|
16
|
-
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
-
spec.metadata["source_code_uri"] = spec.homepage
|
18
|
-
spec.metadata["changelog_uri"] = "https://github.com/xavius-rb/xavius/tree/master/xavius-controllers/CHANGELOG.md"
|
19
|
-
|
20
|
-
# Specify which files should be added to the gem when it is released.
|
21
|
-
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
-
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
23
|
-
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
24
|
-
end
|
25
|
-
spec.bindir = "exe"
|
26
|
-
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
-
spec.require_paths = ["lib"]
|
28
|
-
|
29
|
-
spec.add_development_dependency 'rake', '~> 13.0'
|
30
|
-
spec.add_development_dependency 'rspec-rails', '~> 3'
|
31
|
-
spec.add_development_dependency 'activesupport', '>= 5.0'
|
32
|
-
spec.add_development_dependency 'actionpack', '>= 5.0'
|
33
|
-
end
|