inertia_rails 1.0.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
+ SHA256:
3
+ metadata.gz: 68ba506e9262c9b0ad4d631811f337b4713d103bf2f5c2fc80dce3a8dc688428
4
+ data.tar.gz: 8d1dce1b39abbb1c4bcdef67010ead191bfb83dec3ae2327cfe7fcd3e40191b0
5
+ SHA512:
6
+ metadata.gz: 013501ef4ce007f27d8f16380fda786be161affaa2b46ba42221b2eb3f5568e29e97f2318e9c39e82f86312b8b55b9fc3c58c8a145467b53edf020bf6ca35bf6
7
+ data.tar.gz: 1c20923053db461a39614f988a999fe86272e80f4a89d572bd1419d0b9ddfbdc177c266951c41afacd1033844d81376b6603a8faa80fc0678de80615ca32fe41
data/.gitignore ADDED
@@ -0,0 +1,11 @@
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 ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ before_install: gem install bundler -v 2.0.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at TODO: Write your email address. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in inertia-rails.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 TODO: Bellawatt
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,14 @@
1
+ # Inertia.js Rails Adapter
2
+
3
+ Visit [inertiajs.com](https://inertiajs.com/) to learn more.
4
+
5
+ # Note to pre Rubygems release users
6
+
7
+ The initial version of the gem was named `inertia`; however, that name was not available on Rubygems.
8
+
9
+ The 1.0.0 version release on Rubygems is `intertia_rails`.
10
+
11
+ The changes required are:
12
+
13
+ 1. Use `gem 'inertia_rails'` in your Gemfile (or `gem install inertia_rails`)
14
+ 2. Change any `Inertia.configure` calls to `InertiaRails.configure`
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1 @@
1
+ <div id="app" data-page="<%= page.to_json %>"></div>
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "inertia_rails/rails"
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 ADDED
@@ -0,0 +1,8 @@
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
@@ -0,0 +1,31 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "inertia_rails/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "inertia_rails"
7
+ spec.version = InertiaRails::VERSION
8
+ spec.authors = ["Brian Knoles", "Brandon Shar", "Eugene Granovsky"]
9
+ spec.email = ["brain@bellawatt.com", "brandon@bellawatt.com", "eugene@bellawatt.com"]
10
+
11
+ spec.summary = %q{Inertia adapter for Rails}
12
+ spec.homepage = "https://github.com/inertiajs/inertia-rails/"
13
+ spec.license = "MIT"
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = spec.homepage
17
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/CHANGELOG.md"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_development_dependency "bundler", "~> 2.0"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+ end
@@ -0,0 +1,17 @@
1
+ require 'inertia_rails/renderer'
2
+ require 'inertia_rails/engine'
3
+
4
+ ActionController::Renderers.add :inertia do |component, options|
5
+ InertiaRails::Renderer.new(
6
+ component,
7
+ request,
8
+ response,
9
+ method(:render),
10
+ props: options[:props],
11
+ view_data: options[:view_data],
12
+ ).render
13
+ end
14
+
15
+ module InertiaRails
16
+ class Error < StandardError; end
17
+ end
@@ -0,0 +1,14 @@
1
+ require_relative "inertia_rails"
2
+
3
+ module InertiaRails
4
+ module Controller
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ def inertia_share(**args, &block)
9
+ InertiaRails.share(args) if args
10
+ InertiaRails.share_block(block) if block
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,16 @@
1
+ require_relative "middleware"
2
+ require_relative "controller"
3
+
4
+ module InertiaRails
5
+ class Engine < ::Rails::Engine
6
+ initializer "inertia_rails.configure_rails_initialization" do |app|
7
+ app.middleware.use ::InertiaRails::Middleware
8
+ end
9
+
10
+ initializer "inertia_rails.action_controller" do
11
+ ActiveSupport.on_load(:action_controller) do
12
+ include ::InertiaRails::Controller
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,45 @@
1
+ module InertiaRails
2
+ mattr_accessor :shared_plain_data, default: {}
3
+ mattr_accessor :shared_blocks, default: []
4
+
5
+ def self.configure
6
+ yield(Configuration)
7
+ end
8
+
9
+ # "Getters"
10
+ def self.shared_data
11
+ shared_plain_data.merge!(evaluated_blocks(shared_blocks))
12
+ end
13
+
14
+ def self.version
15
+ Configuration.evaluated_version
16
+ end
17
+
18
+ def self.layout
19
+ Configuration.layout
20
+ end
21
+
22
+ # "Setters"
23
+ def self.share(**args)
24
+ shared_plain_data.merge!(args)
25
+ end
26
+
27
+ def self.share_block(block)
28
+ shared_blocks.push(block)
29
+ end
30
+
31
+ private
32
+
33
+ module Configuration
34
+ mattr_accessor :layout, default: 'application'
35
+ mattr_accessor :version, default: nil
36
+
37
+ def self.evaluated_version
38
+ version.respond_to?(:call) ? version.call : version
39
+ end
40
+ end
41
+
42
+ def self.evaluated_blocks(blocks)
43
+ blocks.flat_map(&:call).reduce(&:merge) || {}
44
+ end
45
+ end
@@ -0,0 +1,44 @@
1
+ module InertiaRails
2
+ class Middleware
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ status, headers, body = @app.call(env)
9
+ request = ActionDispatch::Request.new(env)
10
+
11
+ return [status, headers, body] unless env['HTTP_X_INERTIA'].present?
12
+
13
+ return force_refresh(request) if stale?(env['REQUEST_METHOD'], env['HTTP_X_INERTIA_VERSION'])
14
+
15
+ if is_redirect_status?(status) &&
16
+ is_non_get_redirectable_method?(env['REQUEST_METHOD'])
17
+ status = 303
18
+ end
19
+
20
+ [status, headers, body]
21
+ end
22
+
23
+ private
24
+
25
+ def is_redirect_status?(status)
26
+ [301, 302].include? status
27
+ end
28
+
29
+ def is_non_get_redirectable_method?(request_method)
30
+ ['PUT', 'PATCH', 'DELETE'].include? request_method
31
+ end
32
+
33
+ def stale?(request_method, inertia_version)
34
+ sent_version = InertiaRails.version.is_a?(Numeric) ? inertia_version.to_f : inertia_version
35
+ saved_version = InertiaRails.version.is_a?(Numeric) ? InertiaRails.version.to_f : InertiaRails.version
36
+ request_method == 'GET' && sent_version != saved_version
37
+ end
38
+
39
+ def force_refresh(request)
40
+ request.flash.keep
41
+ Rack::Response.new('', 409, {'X-Inertia-Location' => request.original_url})
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,55 @@
1
+ require_relative "inertia_rails"
2
+
3
+ module InertiaRails
4
+ class Renderer
5
+ attr_reader :component, :view_data
6
+
7
+ def initialize(component, request, response, render_method, props:, view_data:)
8
+ @component = component
9
+ @request = request
10
+ @response = response
11
+ @render_method = render_method
12
+ @props = props || {}
13
+ @view_data = view_data || {}
14
+ end
15
+
16
+ def render
17
+ if @request.headers['X-Inertia']
18
+ @response.set_header('Vary', 'Accept')
19
+ @response.set_header('X-Inertia', 'true')
20
+ @render_method.call json: page, status: 200
21
+ else
22
+ @render_method.call template: 'inertia', layout: ::InertiaRails.layout, locals: (view_data).merge({page: page})
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def props
29
+ only = (@request.headers['X-Inertia-Partial-Data'] || '').split(',').compact.map(&:to_sym)
30
+
31
+ _props = ::InertiaRails.shared_data.merge(@props)
32
+
33
+ _props = (only.any? && @request.headers['X-Inertia-Partial-Component'] == component) ?
34
+ _props.select {|key| key.in? only} :
35
+ _props
36
+
37
+ deep_transform_values(_props, lambda {|prop| prop.respond_to?(:call) ? prop.call : prop })
38
+ end
39
+
40
+ def page
41
+ {
42
+ component: component,
43
+ props: props,
44
+ url: @request.original_url,
45
+ version: ::InertiaRails.version,
46
+ }
47
+ end
48
+
49
+ def deep_transform_values(hash, proc)
50
+ return proc.call(hash) unless hash.is_a? Hash
51
+
52
+ hash.transform_values {|value| deep_transform_values(value, proc)}
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ module InertiaRails
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: inertia_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Brian Knoles
8
+ - Brandon Shar
9
+ - Eugene Granovsky
10
+ autorequire:
11
+ bindir: exe
12
+ cert_chain: []
13
+ date: 2019-10-09 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bundler
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: '2.0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: '2.0'
29
+ - !ruby/object:Gem::Dependency
30
+ name: rake
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - "~>"
34
+ - !ruby/object:Gem::Version
35
+ version: '10.0'
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '10.0'
43
+ - !ruby/object:Gem::Dependency
44
+ name: rspec
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '3.0'
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - "~>"
55
+ - !ruby/object:Gem::Version
56
+ version: '3.0'
57
+ description:
58
+ email:
59
+ - brain@bellawatt.com
60
+ - brandon@bellawatt.com
61
+ - eugene@bellawatt.com
62
+ executables: []
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - ".gitignore"
67
+ - ".rspec"
68
+ - ".travis.yml"
69
+ - CODE_OF_CONDUCT.md
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - app/views/inertia.html.erb
75
+ - bin/console
76
+ - bin/setup
77
+ - inertia_rails.gemspec
78
+ - lib/inertia_rails.rb
79
+ - lib/inertia_rails/controller.rb
80
+ - lib/inertia_rails/engine.rb
81
+ - lib/inertia_rails/inertia_rails.rb
82
+ - lib/inertia_rails/middleware.rb
83
+ - lib/inertia_rails/renderer.rb
84
+ - lib/inertia_rails/version.rb
85
+ homepage: https://github.com/inertiajs/inertia-rails/
86
+ licenses:
87
+ - MIT
88
+ metadata:
89
+ homepage_uri: https://github.com/inertiajs/inertia-rails/
90
+ source_code_uri: https://github.com/inertiajs/inertia-rails/
91
+ changelog_uri: https://github.com/inertiajs/inertia-rails//CHANGELOG.md
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubygems_version: 3.0.3
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Inertia adapter for Rails
111
+ test_files: []