view_component-juice 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 00afc7e9a0144a77ce02acf6a17638c332196f3a20976c8484a77a1dc43b4f2d
4
+ data.tar.gz: 39fbeaca7fb7a65e9d3512953d397e190c11899f21b067d27f58961abdd53feb
5
+ SHA512:
6
+ metadata.gz: beb2ca28cce4494224f21627752188632409e37fd2645481187434364a3fbb111a795cf145e42c04c82631a4b9a41871198da54324fd938ffa2452793881d869
7
+ data.tar.gz: 4a1a8fea4df64e2902324234e88a8e6e5778f04568719934dcb550b48ace54aec67714c5afc365b088af4b53a1c028ec6d94db452c988d900f82088444518d89
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in view_component-juice.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Caleb Owens
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,37 @@
1
+ # ViewComponent::Juice
2
+
3
+ The goal of this gem is to enable writing of interactive view components by levereging the power of turbo, removing the need to write JS.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'view_component-juice'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install view_component-juice
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/calebowens/view_component-juice.
34
+
35
+ ## License
36
+
37
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ViewComponent::Juice::JuiceController < ApplicationController
4
+ def handle_update
5
+ component = juice_params[:component].constantize
6
+
7
+ authenticables = {}
8
+
9
+ if component.authenticate?
10
+ component::AUTHENTICATE.each do |model|
11
+ send "authenticate_#{model}!".to_sym
12
+
13
+ name = "current_#{model}".to_sym
14
+
15
+ authenticables[name] = send(name)
16
+ end
17
+ end
18
+
19
+ component_instance = component.new(context: JSON.parse(juice_params[:context]), **authenticables)
20
+
21
+ component_instance._update(juice_params[:message].to_sym)
22
+
23
+ render component_instance, layout: nil
24
+ end
25
+
26
+ private
27
+
28
+ def juice_params
29
+ params.permit(
30
+ :message,
31
+ :component,
32
+ :context
33
+ )
34
+ end
35
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ViewComponent::Juice::Juicy
4
+ extend ActiveSupport::Concern
5
+
6
+ include Turbo::StreamsHelper
7
+ include Turbo::FramesHelper
8
+
9
+ attr_reader :context
10
+
11
+ class_methods do
12
+ def render(options = {})
13
+ instance = self.new(**options.slice(:context, *authenticate_attributes))
14
+
15
+ instance.setup **options.except(:context, *authenticate_attributes)
16
+
17
+ instance.context['__uuid'] = SecureRandom.uuid
18
+
19
+ instance
20
+ end
21
+
22
+ def messages?
23
+ self.const_defined?(:MESSAGES)
24
+ end
25
+
26
+ def authenticate?
27
+ self.const_defined?(:AUTHENTICATE)
28
+ end
29
+
30
+ def authenticate_attributes
31
+ if authenticate?
32
+ self::AUTHENTICATE.map do |model|
33
+ "current_#{model}".to_sym
34
+ end
35
+ else
36
+ []
37
+ end
38
+ end
39
+ end
40
+
41
+ included do
42
+ def initialize(options)
43
+ @context = options[:context] || {}
44
+
45
+ self.class.authenticate_attributes.each do |attribute|
46
+ self.class.send(:define_method, attribute) { options[attribute] }
47
+ end
48
+ end
49
+
50
+ def frame(&block)
51
+ turbo_frame_tag context['__uuid'], &block
52
+ end
53
+
54
+ def send_message(message)
55
+ if self.class.messages?
56
+ raise "#{message} is not a valid message" unless self.class::MESSAGES.include?(message)
57
+ end
58
+
59
+ Rails.application.routes.url_helpers.juice_path({
60
+ message: message,
61
+ context: JSON.generate(context),
62
+ component: self.class.name
63
+ })
64
+ end
65
+
66
+ def _update(message)
67
+ update(message) if respond_to?(:update)
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ViewComponent
4
+ module Juice
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'juice/juice_controller'
4
+ require_relative 'juice/version'
5
+
6
+ module ViewComponent
7
+ module Juice
8
+ end
9
+ end
@@ -0,0 +1,6 @@
1
+ module ViewComponent
2
+ module Juice
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/view_component/juice/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "view_component-juice"
7
+ spec.version = ViewComponent::Juice::VERSION
8
+ spec.authors = ["Caleb Owens"]
9
+ spec.email = ["hello@calebowens.com"]
10
+
11
+ spec.summary = "Adding an Elm inspired turbo interface into view components"
12
+ spec.homepage = "https://github.com/calebowens/view_compoent-juice"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 2.6.0"
15
+
16
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/calebowens/view_compoent-juice"
20
+ spec.metadata["changelog_uri"] = "https://github.com/calebowens/view_compoent-juice"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0").reject do |f|
26
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
27
+ end
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ # Uncomment to register a new dependency of your gem
34
+ # spec.add_dependency "example-gem", "~> 1.0"
35
+
36
+ # For more information and examples about making a new gem, check out our
37
+ # guide at: https://bundler.io/guides/creating_gem.html
38
+ spec.add_runtime_dependency "turbo-rails", "~> 1.0"
39
+ spec.add_runtime_dependency "view_component", "~> 2.0"
40
+ spec.add_runtime_dependency "activesupport", [">= 5.0.0", "< 8.0"]
41
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: view_component-juice
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Caleb Owens
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-10-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: turbo-rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: view_component
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 5.0.0
48
+ - - "<"
49
+ - !ruby/object:Gem::Version
50
+ version: '8.0'
51
+ type: :runtime
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: 5.0.0
58
+ - - "<"
59
+ - !ruby/object:Gem::Version
60
+ version: '8.0'
61
+ description:
62
+ email:
63
+ - hello@calebowens.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - lib/view_component/juice.rb
73
+ - lib/view_component/juice/juice_controller.rb
74
+ - lib/view_component/juice/juicy.rb
75
+ - lib/view_component/juice/version.rb
76
+ - sig/view_component/juice.rbs
77
+ - view_component-juice.gemspec
78
+ homepage: https://github.com/calebowens/view_compoent-juice
79
+ licenses:
80
+ - MIT
81
+ metadata:
82
+ allowed_push_host: https://rubygems.org
83
+ homepage_uri: https://github.com/calebowens/view_compoent-juice
84
+ source_code_uri: https://github.com/calebowens/view_compoent-juice
85
+ changelog_uri: https://github.com/calebowens/view_compoent-juice
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 2.6.0
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubygems_version: 3.3.7
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Adding an Elm inspired turbo interface into view components
105
+ test_files: []