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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9c3053be7fc49f0734065547ed8aa64977007d6859ab3dfcd880534a731c2bff
4
- data.tar.gz: 51dbe9fb23d2f1f763240bda05862a0cdbd76c3c5f5e546f2515dfda5cb828cc
3
+ metadata.gz: 6e66917d0dc5a3438ba52d547fccda380b3267162686ed5d0232f459e34ea6e2
4
+ data.tar.gz: b3de3703f0892d241ea3423ea3dfa92e164b22404655ac2adec3e3015e457d48
5
5
  SHA512:
6
- metadata.gz: 5cc6e9ef552d694846d9bf4a0de8f8abc2c04c735968c2cadfef578b0acd8e5e98678ec95b260319bc6c1c44cca7a892c79530d2097f4f97bed95e034be6d0e9
7
- data.tar.gz: e0d745ad9eba541749591b35ae8ff1f7f0393f46457282576052e7ab405e92c233b91828ab79adf60c2a39f3caa16f157eddabf64102958e0c0801ef2b5a23ac
6
+ metadata.gz: 34656a9e906407423635ea7c7f7e635a476114dd86819dc5cc326768a3da9dd81d0128a8599e2ca507fa4e80455786a57a8197a02283b6c2436ec2c187af6c90
7
+ data.tar.gz: 302f0ca80e1428e333bfb87cb05e9fe9adb61541adcf24490b4d727a47b288a02171f66b4e1de1956bd057fa93e5eb050012fc2e52941538abb967fe8462c00d
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Easy way to create action classes so you don't get the wrong namespace
3
+
4
+ Example:
5
+ rails generate xavius:action create posts
6
+
7
+ This will create:
8
+ app/actions/posts/create_action.rb
@@ -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,5 @@
1
+ class <%= @namespace.camelize %>::<%= @action_name.camelize %>Action < Xavius::Actions::Base
2
+ def perform
3
+ raise NotImplementedError, "Your action in app/actions/<%= @namespace %>/<%= @action_name %>_action.rb must be implemented"
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ require 'forwardable'
2
+
3
+ module Xavius
4
+ module Actions
5
+ class Base
6
+ extend Forwardable
7
+ def_delegators :@context, :resource_class, :resource, :resource_params
8
+
9
+ def initialize(context)
10
+ @context = context
11
+ end
12
+ end
13
+ end
14
+ 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,9 @@
1
+ module Xavius
2
+ module Actions
3
+ class Create < Base
4
+ def perform
5
+ resource.update(resource_params)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Xavius
2
+ module Actions
3
+ class Destroy < Base
4
+ def perform
5
+ resource.destroy
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Xavius
2
+ module Actions
3
+ class Index < Base
4
+ def perform
5
+ resource_class.all
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Xavius
2
+ module Actions
3
+ class Update < Base
4
+ def perform
5
+ resource.update(resource_params)
6
+ end
7
+ end
8
+ end
9
+ 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
@@ -1,5 +1,5 @@
1
1
  module Xavius
2
2
  module Controllers
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.2"
4
4
  end
5
5
  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.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emerson Xavier
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-29 00:00:00.000000000 Z
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: emersonx.engineer@gmail.com
70
+ email:
71
71
  executables: []
72
72
  extensions: []
73
73
  extra_rdoc_files: []
74
74
  files:
75
- - ".gitignore"
76
- - ".rspec"
77
- - ".travis.yml"
78
- - Gemfile
79
- - README.md
80
- - Rakefile
81
- - bin/console
82
- - bin/setup
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
@@ -1,11 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /_yardoc/
4
- /coverage/
5
- /doc/
6
- /pkg/
7
- /spec/reports/
8
- /tmp/
9
-
10
- # rspec failure tracking
11
- .rspec_status
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --format documentation
2
- --color
3
- --require spec_helper
data/.travis.yml DELETED
@@ -1,6 +0,0 @@
1
- ---
2
- language: ruby
3
- cache: bundler
4
- rvm:
5
- - 2.7.2
6
- before_install: gem install bundler -v 2.1.4
data/Gemfile DELETED
@@ -1,4 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- # Specify your gem's dependencies in xavius-controllers.gemspec
4
- gemspec
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
@@ -1,6 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
-
4
- RSpec::Core::RakeTask.new(:spec)
5
-
6
- task :default => :spec
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
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
@@ -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