rails_react_components 0.1.1 → 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
  SHA1:
3
- metadata.gz: 4bbfe456c56c0a706ed87a338b97ba4198555f4e
4
- data.tar.gz: 8bb44c1352f47e870849dfa74142c912b84ad0da
3
+ metadata.gz: 761524eb75e57ae4e5a0fa67b06e813a03ef3a3b
4
+ data.tar.gz: c12c083b9546e8b297fc69fdbbd53b9591e5eadd
5
5
  SHA512:
6
- metadata.gz: 36b648de21fd051ffa0195aab43ba7c29075d96f36c01f6b14f955d1c4fb160b0a8b6e0b64e1aa60a7d7a4e57c0e24f8cec15aae46a239d5ca2a67fc4eace156
7
- data.tar.gz: a74674431e56181a289ac3350ff6ba57e42053f81f3078b561af315d429f73222c470a75195de2fa62766ea40446f6772b4be941f994993a2808b87663b44325
6
+ metadata.gz: 8210b6d7bca769857da79e3dc3cc2bb39ab565626f88b936d154c05b038cfda2289088ff710d718c05b66c8ced6f5cb4b0bc9d05425ef5758a2a109c5c8c2b6b
7
+ data.tar.gz: 6aa61fc4d898f675982682d2a40c04ccc05c14c42af02e75d63edd43d811bbf5ad88249a6f98c3cff63aba0251e12d6c6d9b298ad96fba6f0c90cf3e830ac4c9
@@ -0,0 +1,25 @@
1
+ AllCops:
2
+ Exclude:
3
+ - spec/**/*
4
+ - bin/**/*
5
+ - Gemfile
6
+ - Rakefile
7
+ - rails_react_components.gemspec
8
+ TargetRubyVersion: 2.4
9
+ Style/Documentation:
10
+ Description: 'Document classes and non-namespace modules.'
11
+ Enabled: false
12
+ Style/StringLiterals:
13
+ EnforcedStyle: double_quotes
14
+ SupportedStyles:
15
+ - double_quotes
16
+ Style/PercentLiteralDelimiters:
17
+ PreferredDelimiters:
18
+ default: ()
19
+ '%i': '()'
20
+ '%I': '()'
21
+ '%r': '{}'
22
+ '%w': '()'
23
+ '%W': '()'
24
+ Metrics/LineLength:
25
+ Max: 100
@@ -2,4 +2,9 @@ sudo: false
2
2
  language: ruby
3
3
  rvm:
4
4
  - 2.4.0
5
- before_install: gem install bundler -v 1.14.6
5
+ before_install:
6
+ - gem install bundler -v 1.14.6
7
+ - gem install rubocop
8
+ script:
9
+ - rspec spec
10
+ - rubocop
data/README.md CHANGED
@@ -1,11 +1,52 @@
1
1
  # RailsReactComponents
2
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/rails_react_components`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ [![Build Status](https://travis-ci.org/Adam-Stomski/rails_react_components.svg?branch=master)](https://travis-ci.org/Adam-Stomski/rails_react_components)
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ Component objects for Rails and React with `react_on_rails`
6
+
7
+ ```ruby
8
+ # component class
9
+ class MyComponent < RailsReactComponents::Component
10
+ prop :name
11
+ prop :email, on: :user
12
+ prop :is_ready
13
+ prop :received_email, as: :got_email
14
+
15
+ def name
16
+ "My name is Rails Component"
17
+ end
18
+
19
+ def is_ready
20
+ user.ready?
21
+ end
22
+
23
+ def received_email
24
+ false
25
+ end
26
+ end
27
+
28
+ # in view this can run:
29
+ # react_component("MyComponent",
30
+ # props: {
31
+ # name: "My name is Rails Component",
32
+ # email: "user@example.com",
33
+ # isReady: true,
34
+ # got_email: false
35
+ # }
36
+ # )
37
+ render_react_component MyComponent, user: current_user
38
+ ```
39
+
40
+ ## TODO
41
+
42
+ - [ ] - nested props
43
+ - [ ] - Rspec matchers
44
+ - [x] - configuration (snake_case methods for camelCase props etc)
6
45
 
7
46
  ## Installation
8
47
 
48
+ If you are not using `react_on_rails` yet check https://github.com/shakacode/react_on_rails
49
+
9
50
  Add this line to your application's Gemfile:
10
51
 
11
52
  ```ruby
@@ -20,10 +61,49 @@ Or install it yourself as:
20
61
 
21
62
  $ gem install rails_react_components
22
63
 
64
+ ## Configuration
65
+
66
+ You can add initializer (these are defaults):
67
+
68
+ ```ruby
69
+ RailsReactComponents.config do |config|
70
+ config.camelize_props = true # method :some_method will end up someMethod prop
71
+ # when true, only way to prevent this is using prop as: option
72
+ config.prerender = nil # set global prerender
73
+ config.raise_on_prerender_error = nil # set global raise_on_prerender_error
74
+ end
75
+ ```
76
+
23
77
  ## Usage
24
78
 
25
- TODO: Write usage instructions here
26
79
 
80
+ #### Available options example:
81
+ ```ruby
82
+ class MyComponent < RailsReactComponents::Component
83
+ component "FilePicker"
84
+
85
+ id "file-picker-id"
86
+
87
+ html_option "color", "red"
88
+ html_option "opacity", "0.5"
89
+
90
+ prerender
91
+ trace
92
+ replay_console
93
+ raise_on_prerender_error
94
+
95
+ prop :blank, include_blank: false
96
+
97
+ prop :name, on: :user
98
+ prop :email, delegate: :user # alias
99
+
100
+ def blank
101
+ ""
102
+ end
103
+ end
104
+ ```
105
+
106
+ check also [here](spec/features) and [here](https://github.com/shakacode/react_on_rails#react_component)
27
107
 
28
108
  ## Contributing
29
109
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RailsReactComponentsHelper
2
4
  def render_react_component(klass, options = {})
3
5
  component = klass.new(options)
@@ -1,7 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "rails"
2
4
  require "rails_react_components/engine"
3
5
 
4
6
  module RailsReactComponents
7
+ mattr_accessor :camelize_props
8
+ self.camelize_props = true
9
+
10
+ mattr_accessor :prerender
11
+ self.prerender = nil
12
+
13
+ mattr_accessor :raise_on_prerender_error
14
+ self.raise_on_prerender_error = nil
15
+
16
+ def self.config
17
+ yield self
18
+ end
5
19
  end
6
20
 
7
21
  require "rails_react_components/version"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative "components/prop"
2
4
  require_relative "components/dsl"
3
5
 
@@ -18,7 +20,7 @@ module RailsReactComponents
18
20
  end
19
21
 
20
22
  def respond_to_missing?(method_name, include_private = false)
21
- _methods.has_key?(method_name.to_sym) || super
23
+ _methods.key?(method_name.to_sym) || super
22
24
  end
23
25
 
24
26
  def component_options
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RailsReactComponents
2
4
  module Components
3
5
  module Dsl
@@ -33,7 +35,17 @@ module RailsReactComponents
33
35
  html_options[name] = value
34
36
  end
35
37
 
36
- %w(prerender trace replay_console raise_on_prerender_error).each do |method|
38
+ %w(prerender raise_on_prerender_error).each do |method|
39
+ define_method method do
40
+ instance_variable_set("@_#{method}", true)
41
+ end
42
+
43
+ define_method "#{method}?" do
44
+ instance_variable_get("@_#{method}") || RailsReactComponents.send(method)
45
+ end
46
+ end
47
+
48
+ %w(trace replay_console).each do |method|
37
49
  define_method method do
38
50
  instance_variable_set("@_#{method}", true)
39
51
  end
@@ -1,18 +1,18 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RailsReactComponents
2
4
  module Components
3
5
  class Prop
4
- attr_reader :name
5
-
6
- def initialize(name, options)
7
- @name = name.to_sym
6
+ def initialize(method_name, options)
7
+ @method_name = method_name.to_sym
8
8
  @options = options
9
9
  end
10
10
 
11
11
  def build(component)
12
12
  if on.present?
13
- component.send(on).send(name)
13
+ component.send(on).send(method_name)
14
14
  else
15
- component.send(name)
15
+ component.send(method_name)
16
16
  end
17
17
  end
18
18
 
@@ -20,12 +20,26 @@ module RailsReactComponents
20
20
  options.fetch(:include_blank, true)
21
21
  end
22
22
 
23
+ def name
24
+ if as.present?
25
+ as
26
+ elsif RailsReactComponents.camelize_props
27
+ method_name.to_s.camelize(:lower).to_sym
28
+ else
29
+ method_name
30
+ end
31
+ end
32
+
23
33
  private
24
34
 
25
- attr_reader :options
35
+ attr_reader :options, :method_name
26
36
 
27
37
  def on
28
- @on ||= options[:on] || options[:delegate]
38
+ options[:on] || options[:delegate]
39
+ end
40
+
41
+ def as
42
+ options.fetch(:as, nil)&.to_sym
29
43
  end
30
44
  end
31
45
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RailsReactComponents
2
4
  class Engine < Rails::Engine
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RailsReactComponents
2
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
3
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_react_components
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Stomski
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-04-06 00:00:00.000000000 Z
11
+ date: 2017-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -89,6 +89,7 @@ extra_rdoc_files: []
89
89
  files:
90
90
  - ".gitignore"
91
91
  - ".rspec"
92
+ - ".rubocop.yml"
92
93
  - ".travis.yml"
93
94
  - Gemfile
94
95
  - LICENSE.txt