3 |
4 |
--------------------------------------------------------------------------------
/spec/dummy/config/boot.rb:
--------------------------------------------------------------------------------
1 | # Set up gems listed in the Gemfile.
2 | ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__)
3 |
4 | require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
5 | $LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__)
6 |
--------------------------------------------------------------------------------
/spec/dummy/Rakefile:
--------------------------------------------------------------------------------
1 | # Add your own tasks in files placed in lib/tasks ending in .rake,
2 | # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3 |
4 | require File.expand_path('../config/application', __FILE__)
5 |
6 | Rails.application.load_tasks
7 |
--------------------------------------------------------------------------------
/bin/console:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env ruby
2 |
3 | require "bundler/setup"
4 | require "react/rails/img"
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
15 |
--------------------------------------------------------------------------------
/spec/dummy/app/views/layouts/application.html.erb:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Dummy
5 | <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
6 | <%= javascript_include_tag 'application', 'data-turbolinks-track' => true, 'debug' => params[:debug_assets] %>
7 | <%= csrf_meta_tags %>
8 |
9 |
10 |
11 | <%= yield %>
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/spec/support/capybara.rb:
--------------------------------------------------------------------------------
1 | require "capybara/webkit"
2 |
3 | Capybara.javascript_driver = if ENV['JAVASCRIPT_DRIVER'] # can be "selenium" or "webkit"
4 | ENV['JAVASCRIPT_DRIVER'].to_sym
5 | else
6 | :webkit # as defult javascript_driver
7 | end
8 |
9 | # fail fast
10 | def expect_no_js_errors
11 | if Capybara.current_driver == :webkit || Capybara.current_driver == :webkit_debug
12 | expect(page.driver.error_messages).to be_empty
13 | end
14 | end
15 |
--------------------------------------------------------------------------------
/spec/dummy/config/initializers/backtrace_silencers.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
4 | # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
5 |
6 | # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
7 | # Rails.backtrace_cleaner.remove_silencers!
8 |
--------------------------------------------------------------------------------
/spec/dummy/config/initializers/assets.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | # Version of your assets, change this if you want to expire all your assets.
4 | Rails.application.config.assets.version = '1.0'
5 |
6 | # Add additional assets to the asset load path
7 | # Rails.application.config.assets.paths << Emoji.images_path
8 |
9 | # Precompile additional assets.
10 | # application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
11 | # Rails.application.config.assets.precompile += %w( search.js )
12 |
--------------------------------------------------------------------------------
/spec/react/rails/img_spec.rb:
--------------------------------------------------------------------------------
1 | require 'rails_helper'
2 |
3 | describe React::Rails::Img, type: :request do
4 | it 'should be working with asset pipline' do
5 | get '/assets/application.js'
6 | expect(response).to be_success
7 |
8 | # enable to load by Sprockets
9 | expect(response.body).not_to match "Sprockets::FileNotFound"
10 |
11 | # no syntax error
12 | expect(response.body).not_to match "ExecJS::RuntimeError: SyntaxError"
13 |
14 | # got the correct content
15 | expect(response.body).to match "var Img = React.createClass"
16 | end
17 | end
18 |
--------------------------------------------------------------------------------
/spec/support/debug_helper.rb:
--------------------------------------------------------------------------------
1 | require 'byebug'
2 |
3 | # add some shortcuts methods
4 | # http://www.rubydoc.info/gems/capybara#Debugging
5 | def page!
6 | puts save_and_open_page
7 | puts save_and_open_screenshot
8 |
9 | puts "[DEBUG]page.html:"
10 | puts page.html
11 |
12 | if Capybara.current_driver == :webkit
13 | puts "[DEBUG]console_messages:"
14 | puts page.driver.console_messages
15 | # puts page.driver.error_messages
16 | end
17 |
18 | if Capybara.current_driver == :poltergeist
19 | page.driver.render('tmp/page.png') && `open tmp/page.png`
20 | end
21 | end
22 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: ruby
2 | rvm:
3 | - 2.1.9
4 | - 2.2.5
5 | - 2.3.3
6 | env:
7 | global:
8 | # - JAVASCRIPT_DRIVER=webkit_debug
9 |
10 | # to get capybara-webkit work with qt5
11 | # refs:
12 | # - http://stackoverflow.com/questions/37201085/unable-to-locate-package-libqt5webkit5-dev
13 | # - https://docs.travis-ci.com/user/trusty-ci-environment#tl%3Bdr---Using-Trusty
14 | sudo: required
15 | dist: trusty
16 | addons:
17 | apt:
18 | sources:
19 | - ubuntu-sdk-team
20 | packages:
21 | - libqt5webkit5-dev
22 | - qtdeclarative5-dev
23 | script: xvfb-run bundle exec rake
24 |
--------------------------------------------------------------------------------
/spec/dummy/config/initializers/wrap_parameters.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | # This file contains settings for ActionController::ParamsWrapper which
4 | # is enabled by default.
5 |
6 | # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
7 | ActiveSupport.on_load(:action_controller) do
8 | wrap_parameters format: [:json] if respond_to?(:wrap_parameters)
9 | end
10 |
11 | # To enable root element in JSON for ActiveRecord objects.
12 | # ActiveSupport.on_load(:active_record) do
13 | # self.include_root_in_json = true
14 | # end
15 |
--------------------------------------------------------------------------------
/spec/dummy/README.rdoc:
--------------------------------------------------------------------------------
1 | == README
2 |
3 | This README would normally document whatever steps are necessary to get the
4 | application up and running.
5 |
6 | Things you may want to cover:
7 |
8 | * Ruby version
9 |
10 | * System dependencies
11 |
12 | * Configuration
13 |
14 | * Database creation
15 |
16 | * Database initialization
17 |
18 | * How to run the test suite
19 |
20 | * Services (job queues, cache servers, search engines, etc.)
21 |
22 | * Deployment instructions
23 |
24 | * ...
25 |
26 |
27 | Please feel free to use a different markup language if you do not plan to run
28 | rake doc:app.
29 |
--------------------------------------------------------------------------------
/spec/dummy/config/database.yml:
--------------------------------------------------------------------------------
1 | # SQLite version 3.x
2 | # gem install sqlite3
3 | #
4 | # Ensure the SQLite 3 gem is defined in your Gemfile
5 | # gem 'sqlite3'
6 | #
7 | default: &default
8 | adapter: sqlite3
9 | pool: 5
10 | timeout: 5000
11 |
12 | development:
13 | <<: *default
14 | database: db/development.sqlite3
15 |
16 | # Warning: The database defined as "test" will be erased and
17 | # re-generated from your development database when you run "rake".
18 | # Do not set this db to the same as development or production.
19 | test:
20 | <<: *default
21 | database: db/test.sqlite3
22 |
23 | production:
24 | <<: *default
25 | database: db/production.sqlite3
26 |
--------------------------------------------------------------------------------
/spec/dummy/config/initializers/inflections.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | # Add new inflection rules using the following format. Inflections
4 | # are locale specific, and you may define rules for as many different
5 | # locales as you wish. All of these examples are active by default:
6 | # ActiveSupport::Inflector.inflections(:en) do |inflect|
7 | # inflect.plural /^(ox)$/i, '\1en'
8 | # inflect.singular /^(ox)en/i, '\1'
9 | # inflect.irregular 'person', 'people'
10 | # inflect.uncountable %w( fish sheep )
11 | # end
12 |
13 | # These inflection rules are supported but not enabled by default:
14 | # ActiveSupport::Inflector.inflections(:en) do |inflect|
15 | # inflect.acronym 'RESTful'
16 | # end
17 |
--------------------------------------------------------------------------------
/spec/dummy/config/locales/en.yml:
--------------------------------------------------------------------------------
1 | # Files in the config/locales directory are used for internationalization
2 | # and are automatically loaded by Rails. If you want to use locales other
3 | # than English, add the necessary files in this directory.
4 | #
5 | # To use the locales, use `I18n.t`:
6 | #
7 | # I18n.t 'hello'
8 | #
9 | # In views, this is aliased to just `t`:
10 | #
11 | # <%= t('hello') %>
12 | #
13 | # To use a different locale, set it with `I18n.locale`:
14 | #
15 | # I18n.locale = :es
16 | #
17 | # This would use the information in config/locales/es.yml.
18 | #
19 | # To learn more, please read the Rails Internationalization guide
20 | # available at http://guides.rubyonrails.org/i18n.html.
21 |
22 | en:
23 | hello: "Hello world"
24 |
--------------------------------------------------------------------------------
/spec/dummy/app/assets/stylesheets/application.css:
--------------------------------------------------------------------------------
1 | /*
2 | * This is a manifest file that'll be compiled into application.css, which will include all the files
3 | * listed below.
4 | *
5 | * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6 | * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7 | *
8 | * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9 | * compiled file so the styles you add here take precedence over styles defined in any styles
10 | * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11 | * file per style scope.
12 | *
13 | *= require_tree .
14 | *= require_self
15 | */
16 |
--------------------------------------------------------------------------------
/spec/dummy/app/assets/javascripts/application.js:
--------------------------------------------------------------------------------
1 | // This is a manifest file that'll be compiled into application.js, which will include all the files
2 | // listed below.
3 | //
4 | // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5 | // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6 | //
7 | // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8 | // compiled file.
9 | //
10 | // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11 | // about supported directives.
12 | //
13 | //= require react
14 | //= require react_ujs
15 | //= require components
16 | //= require react_rails_img
17 | //= require_tree .
18 |
--------------------------------------------------------------------------------
/spec/dummy/bin/setup:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env ruby
2 | require 'pathname'
3 |
4 | # path to your application root.
5 | APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
6 |
7 | Dir.chdir APP_ROOT do
8 | # This script is a starting point to setup your application.
9 | # Add necessary setup steps to this file:
10 |
11 | puts "== Installing dependencies =="
12 | system "gem install bundler --conservative"
13 | system "bundle check || bundle install"
14 |
15 | # puts "\n== Copying sample files =="
16 | # unless File.exist?("config/database.yml")
17 | # system "cp config/database.yml.sample config/database.yml"
18 | # end
19 |
20 | puts "\n== Preparing database =="
21 | system "bin/rake db:setup"
22 |
23 | puts "\n== Removing old logs and tempfiles =="
24 | system "rm -f log/*"
25 | system "rm -rf tmp/cache"
26 |
27 | puts "\n== Restarting application server =="
28 | system "touch tmp/restart.txt"
29 | end
30 |
--------------------------------------------------------------------------------
/spec/dummy/config/secrets.yml:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | # Your secret key is used for verifying the integrity of signed cookies.
4 | # If you change this key, all old signed cookies will become invalid!
5 |
6 | # Make sure the secret is at least 30 characters and all random,
7 | # no regular words or you'll be exposed to dictionary attacks.
8 | # You can use `rake secret` to generate a secure secret key.
9 |
10 | # Make sure the secrets in this file are kept private
11 | # if you're sharing your code publicly.
12 |
13 | development:
14 | secret_key_base: 788f44183b1d2d7acd6cf0da0f3121722f36a5bc4b9c882779a51752d29238b10cb642b83df42ee1af34bf4060e41672320dba0238bcddbb648f8ea6b14b4b1f
15 |
16 | test:
17 | secret_key_base: ac72c36890b5df7ae4af566977027898b734f63c19b989f20cf84d78a1f1b3609b6584824db3ed9471ea02760a4f2b3100cf1362594de0e00682ab3997d83ce6
18 |
19 | # Do not keep production secrets in the repository,
20 | # instead read values from the environment.
21 | production:
22 | secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
23 |
--------------------------------------------------------------------------------
/spec/dummy/config/application.rb:
--------------------------------------------------------------------------------
1 | require File.expand_path('../boot', __FILE__)
2 |
3 | # Pick the frameworks you want:
4 | require "active_record/railtie"
5 | require "action_controller/railtie"
6 | require "action_mailer/railtie"
7 | require "action_view/railtie"
8 | require "sprockets/railtie"
9 | # require "rails/test_unit/railtie"
10 |
11 | Bundler.require(*Rails.groups)
12 | require "react-rails"
13 |
14 | module Dummy
15 | class Application < Rails::Application
16 | # Settings in config/environments/* take precedence over those specified here.
17 | # Application configuration should go into files in config/initializers
18 | # -- all .rb files in that directory are automatically loaded.
19 |
20 | # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
21 | # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
22 | # config.time_zone = 'Central Time (US & Canada)'
23 |
24 | # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
25 | # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
26 | # config.i18n.default_locale = :de
27 |
28 | # Do not swallow errors in after_commit/after_rollback callbacks.
29 | config.active_record.raise_in_transactional_callbacks = true
30 | end
31 | end
32 |
33 |
--------------------------------------------------------------------------------
/react-rails-img.gemspec:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | lib = File.expand_path('../lib', __FILE__)
3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4 | require 'react/rails/img/version'
5 |
6 | Gem::Specification.new do |spec|
7 | spec.name = "react-rails-img"
8 | spec.version = React::Rails::Img::VERSION
9 | spec.authors = ["RainChen"]
10 | spec.email = ["hirainchen@gmail.com"]
11 |
12 | spec.summary = %q{simple image helpers for the rails project using react}
13 | spec.description = %q{simple image helpers for the rails project using react, make it easily just like using the tag}
14 | spec.homepage = "https://github.com/rainchen/react-rails-img"
15 | spec.license = "MIT"
16 |
17 | spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18 | spec.bindir = "exe"
19 | spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20 | spec.require_paths = ["lib"]
21 |
22 | spec.add_dependency "react-rails"
23 |
24 | spec.add_development_dependency "bundler", "~> 1.9"
25 | spec.add_development_dependency "rake", "~> 10.0"
26 |
27 | # for testing
28 | spec.add_development_dependency "rails", "~> 4.2.7"
29 | spec.add_development_dependency "sqlite3"
30 | spec.add_development_dependency "rspec-rails", "~> 3.5"
31 | spec.add_development_dependency "capybara-webkit"
32 | spec.add_development_dependency "selenium-webdriver"
33 | spec.add_development_dependency "launchy"
34 | spec.add_development_dependency "byebug"
35 | end
36 |
--------------------------------------------------------------------------------
/spec/dummy/app/assets/javascripts/components/post.js.jsx:
--------------------------------------------------------------------------------
1 | // rails generate react:component Post title:string body:string published:bool published_by:instanceOf{Person}
2 | var Post = React.createClass({
3 | propTypes: {
4 | title: React.PropTypes.string,
5 | body: React.PropTypes.string,
6 | published: React.PropTypes.bool,
7 | publishedBy: React.PropTypes.node
8 | },
9 |
10 | getInitialState: function(){
11 | return {clicked: false};
12 | },
13 |
14 | handleClick: function(){
15 | this.setState({clicked: true});
16 | },
17 |
18 | render: function() {
19 | var railsLogo = "rails-logo.svg";
20 | var reactjsLogo = "logo/reactjs-logo.svg";
21 | return (
22 |
If you are the application owner check the logs for more information.
64 |
65 |
66 |
67 |
--------------------------------------------------------------------------------
/spec/dummy/config/environments/development.rb:
--------------------------------------------------------------------------------
1 | Rails.application.configure do
2 | # Settings specified here will take precedence over those in config/application.rb.
3 |
4 | # In the development environment your application's code is reloaded on
5 | # every request. This slows down response time but is perfect for development
6 | # since you don't have to restart the web server when you make code changes.
7 | config.cache_classes = false
8 |
9 | # Do not eager load code on boot.
10 | config.eager_load = false
11 |
12 | # Show full error reports and disable caching.
13 | config.consider_all_requests_local = true
14 | config.action_controller.perform_caching = false
15 |
16 | # Don't care if the mailer can't send.
17 | config.action_mailer.raise_delivery_errors = false
18 |
19 | # Print deprecation notices to the Rails logger.
20 | config.active_support.deprecation = :log
21 |
22 | # Raise an error on page load if there are pending migrations.
23 | config.active_record.migration_error = :page_load
24 |
25 | # Debug mode disables concatenation and preprocessing of assets.
26 | # This option may cause significant delays in view rendering with a large
27 | # number of complex assets.
28 | config.assets.debug = true
29 |
30 | # Asset digests allow you to set far-future HTTP expiration dates on all assets,
31 | # yet still be able to expire them through the digest params.
32 | config.assets.digest = true
33 |
34 | # Adds additional error checking when serving assets at runtime.
35 | # Checks for improperly declared sprockets dependencies.
36 | # Raises helpful error messages.
37 | config.assets.raise_runtime_errors = true
38 |
39 | # Raises error for missing translations
40 | # config.action_view.raise_on_missing_translations = true
41 | end
42 |
--------------------------------------------------------------------------------
/spec/dummy/public/422.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | The change you wanted was rejected (422)
5 |
6 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
The change you wanted was rejected.
62 |
Maybe you tried to change something you didn't have access to.
63 |
64 |
If you are the application owner check the logs for more information.
65 |
66 |
67 |
68 |
--------------------------------------------------------------------------------
/spec/dummy/public/404.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | The page you were looking for doesn't exist (404)
5 |
6 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
The page you were looking for doesn't exist.
62 |
You may have mistyped the address or the page may have moved.
63 |
64 |
If you are the application owner check the logs for more information.
65 |
66 |
67 |
68 |
--------------------------------------------------------------------------------
/spec/dummy/app/assets/images/logo/reactjs-logo.svg:
--------------------------------------------------------------------------------
1 |
2 |
3 |
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # react-rails-img
2 |
3 | Simple image helpers for the rails project using react, make it easily just like using the `` tag.
4 |
5 | [](https://travis-ci.org/rainchen/react-rails-img)
6 | [](https://rubygems.org/gems/react-rails-img)
7 |
8 | ## Installation
9 |
10 | 1. Add this line to your application's Gemfile:
11 |
12 | ```ruby
13 | gem 'react-rails-img'
14 | ```
15 |
16 | And then execute:
17 |
18 | $ bundle
19 |
20 | 2. Require the javascript file in `app/assets/javascripts/application.js`:
21 |
22 | ```js
23 | //= require react_rails_img
24 | ```
25 |
26 | ## Usage
27 |
28 | ### React Component Style
29 |
30 | the asset adds a `` component
31 |
32 | 1. base usage: e.g.: ``
33 | 2. with props: e.g.: ``
34 | 3. advance usage - using cssSprite: e.g.: ``
35 |
36 | 1) this feature requires gem 'css_sprite'
37 |
38 | 2) css_sprite images should be placed in dir `assets/images/css_sprite/`
39 |
40 | 3) put the string `css_sprite` after `/assets/` in the path
41 |
42 | ### JS funtion Style
43 |
44 | just similar to rails helper `image_tag`
45 |
46 | 1. base usage: e.g.: `imageTag('logo.png')`
47 | 2. with props: e.g.: `imageTag('path/logo.png', {alt: 'logo', className: 'logo', id: 'logo', width: 100, height: 50})`
48 |
49 |
50 | ### helper for getting image path
51 |
52 | e.g.: `Img.assetPath('placeholder/logo.png')`
53 |
54 | ## Acknowledgements
55 |
56 | the image path will respect `Rails.env`, for development it will be sth like
57 |
58 | `/assets/logo.png`
59 |
60 | and for production, it will be contains the timestamp as
61 |
62 | `/assets/logo-be1f67ffd42a4c1a41bdcc547c5705a3423a2f24bfe930f00398077fe518e6c0.png`
63 |
64 |
65 | ## Contributing
66 |
67 | 1. Fork it ( https://github.com/rainchen/react-rails-img/fork )
68 | 2. Create your feature branch (`git checkout -b my-new-feature`)
69 | 3. Commit your changes (`git commit -am 'Add some feature'`)
70 | 4. Push to the branch (`git push origin my-new-feature`)
71 | 5. Create a new Pull Request
72 |
--------------------------------------------------------------------------------
/spec/dummy/config/environments/test.rb:
--------------------------------------------------------------------------------
1 | Rails.application.configure do
2 | # Settings specified here will take precedence over those in config/application.rb.
3 |
4 | # The test environment is used exclusively to run your application's
5 | # test suite. You never need to work with it otherwise. Remember that
6 | # your test database is "scratch space" for the test suite and is wiped
7 | # and recreated between test runs. Don't rely on the data there!
8 | config.cache_classes = true
9 | # config.cache_classes = false # set cache_classes to false to disable Sprockets caching assets so that `Sprockets.register_dependency_resolver 'rails-env'` can be called for each asset
10 | # prevent sprockets from caching assets in test env when cache_classes is set to true
11 | sprockets_env = nil
12 | config.assets.configure do |env|
13 | sprockets_env = env # Sprockets::Environment
14 | # Use memory store for assets cache in test env to avoid caching to tmp/assets
15 | env.cache = ActiveSupport::Cache.lookup_store(:memory_store)
16 | end
17 | # reset `Rails.application.assets` which was set by sprockets-rails using `env = env.cached; app.assets = self.build_environment(app, true)`
18 | # in sprockets-rails-3.1.1/lib/sprockets/railtie.rb
19 | config.after_initialize do
20 | # Rails.application.assets is Sprockets::CachedEnvironment
21 | Rails.application.assets = sprockets_env
22 | end
23 |
24 | # Do not eager load code on boot. This avoids loading your whole application
25 | # just for the purpose of running a single test. If you are using a tool that
26 | # preloads Rails for running tests, you may have to set it to true.
27 | config.eager_load = false
28 |
29 | # Configure static file server for tests with Cache-Control for performance.
30 | config.serve_static_files = true
31 | config.static_cache_control = 'public, max-age=3600'
32 |
33 | # Show full error reports and disable caching.
34 | config.consider_all_requests_local = true
35 | config.action_controller.perform_caching = false
36 |
37 | # Raise exceptions instead of rendering exception templates.
38 | config.action_dispatch.show_exceptions = false
39 |
40 | # Disable request forgery protection in test environment.
41 | config.action_controller.allow_forgery_protection = false
42 |
43 | # Tell Action Mailer not to deliver emails to the real world.
44 | # The :test delivery method accumulates sent emails in the
45 | # ActionMailer::Base.deliveries array.
46 | config.action_mailer.delivery_method = :test
47 |
48 | # Randomize the order test cases are executed.
49 | config.active_support.test_order = :random
50 |
51 | # Print deprecation notices to the stderr.
52 | config.active_support.deprecation = :stderr
53 |
54 | # Raises error for missing translations
55 | # config.action_view.raise_on_missing_translations = true
56 | end
57 |
--------------------------------------------------------------------------------
/spec/rails_helper.rb:
--------------------------------------------------------------------------------
1 | # This file is copied to spec/ when you run 'rails generate rspec:install'
2 | ENV['RAILS_ENV'] ||= 'test'
3 | require File.expand_path('../dummy/config/environment', __FILE__)
4 | # Prevent database truncation if the environment is production
5 | abort("The Rails environment is running in production mode!") if Rails.env.production?
6 | require 'spec_helper'
7 | require 'rspec/rails'
8 | # Add additional requires below this line. Rails is not loaded until this point!
9 |
10 | # Requires supporting ruby files with custom matchers and macros, etc, in
11 | # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
12 | # run as spec files by default. This means that files in spec/support that end
13 | # in _spec.rb will both be required and run as specs, causing the specs to be
14 | # run twice. It is recommended that you do not name files matching this glob to
15 | # end with _spec.rb. You can configure this pattern with the --pattern
16 | # option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
17 | #
18 | # The following line is provided for convenience purposes. It has the downside
19 | # of increasing the boot-up time by auto-requiring all files in the support
20 | # directory. Alternatively, in the individual `*_spec.rb` files, manually
21 | # require only the support files necessary.
22 | #
23 | # Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
24 |
25 | # Checks for pending migration and applies them before tests are run.
26 | # If you are not using ActiveRecord, you can remove this line.
27 | ActiveRecord::Migration.maintain_test_schema!
28 |
29 | RSpec.configure do |config|
30 | # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
31 | config.fixture_path = "#{::Rails.root}/spec/fixtures"
32 |
33 | # If you're not using ActiveRecord, or you'd prefer not to run each of your
34 | # examples within a transaction, remove the following line or assign false
35 | # instead of true.
36 | config.use_transactional_fixtures = true
37 |
38 | # RSpec Rails can automatically mix in different behaviours to your tests
39 | # based on their file location, for example enabling you to call `get` and
40 | # `post` in specs under `spec/controllers`.
41 | #
42 | # You can disable this behaviour by removing the line below, and instead
43 | # explicitly tag your specs with their type, e.g.:
44 | #
45 | # RSpec.describe UsersController, :type => :controller do
46 | # # ...
47 | # end
48 | #
49 | # The different available types are documented in the features, such as in
50 | # https://relishapp.com/rspec/rspec-rails/docs
51 | config.infer_spec_type_from_file_location!
52 |
53 | # Filter lines from Rails gems in backtraces.
54 | config.filter_rails_from_backtrace!
55 | # arbitrary gems may also be filtered via:
56 | # config.filter_gems_from_backtrace("gem name")
57 | end
58 |
--------------------------------------------------------------------------------
/spec/dummy/app/assets/images/rails-logo.svg:
--------------------------------------------------------------------------------
1 |
2 |
36 |
--------------------------------------------------------------------------------
/vendor/assets/javascripts/react_rails_img.js.jsx.erb:
--------------------------------------------------------------------------------
1 | <%
2 | images = {}
3 | prefix = Rails.application.config.assets.prefix
4 | should_compute_assets_path = Rails.env.production? || Rails.env.staging? # for performance
5 | if should_compute_assets_path
6 | images_path = Rails.root.join('app/assets/images/')
7 | images_dir = images_path.to_s
8 | images_enumerator = defined?(assets) ? assets.each_file : Dir["#{images_dir}**/*.*"]
9 | images_enumerator.each do |file_path|
10 | if file_path.to_s.starts_with?(images_dir)
11 | relative_path = Pathname.new(file_path).relative_path_from(Pathname.new(images_path)).to_s
12 | file_path = asset_path(relative_path)
13 | images[relative_path] = file_path
14 | end
15 | end
16 | end
17 | %>
18 | // usage:
19 | // using cssSprite: =>
20 | // , where-b.png should be placed in `assets/images/css_sprite`
21 | var Img = React.createClass({
22 | statics: {
23 | // usage: Img.assetPath('logo.png') => "/assets/logo.png"
24 | assetPath: function (path) {
25 | var paths = <%= images.to_json %>;
26 | return paths[path] || "/assets/"+path;
27 | }
28 | },
29 |
30 | propTypes: {
31 | src: React.PropTypes.string
32 | },
33 |
34 | src: function(){
35 | var src = this.props.src;
36 | if(src.match("/assets/")){
37 | src = src.replace("/assets/", '');
38 | }
39 | src = Img.assetPath(src);
40 | return src;
41 | },
42 |
43 | isUsingCssSprite: function (src) {
44 | return src.match(/css_sprite/);
45 | },
46 |
47 | useCssSprite: function (src, className) {
48 | // "/assets/ui-right-w.png" => "ui-right-w"
49 | var baseName = src.replace(/\/assets\/(.*)\..*$/, '$1');
50 | var cssSpriteClassName = baseName.replace(/css_sprite\//, '');
51 | if(className){
52 | className = cssSpriteClassName + " "+className;
53 | }else{
54 | className = cssSpriteClassName;
55 | }
56 | src = Img.assetPath('css_sprite_holder.png');
57 | //src = null; // disable the src
58 | return {src: src, className: className};
59 | },
60 |
61 | render: function() {
62 | var className = this.props.className;
63 | var src = this.src();
64 | // using css_sprite
65 | if(this.isUsingCssSprite(src)){
66 | // src should be url like "/assets/css_sprite/xxx.png"
67 | var result = this.useCssSprite(this.props.src, className);
68 | src = result.src;
69 | className = result.className;
70 | }
71 | var props = {src: src};
72 | if(className){
73 | props.className = className;
74 | }
75 | return ;
76 | }
77 | });
78 |
79 |
80 | // similar to rails helper `image_tag`, e.g.: {imageTag('placeholder/active-01.png')}
81 | // the function will return for development:
82 | // /assets/placeholder/active-01.png
83 | // and for production
84 | // /assets/placeholder/active-01-be1f67ffd42a4c1a41bdcc547c5705a3423a2f24bfe930f00398077fe518e6c0.png
85 | function imageTag (source, options) {
86 | if(!options)options = {};
87 | var src = "/assets/"+source;
88 | return ;
89 | }
90 |
--------------------------------------------------------------------------------
/spec/react/rails/img_js_features_spec.rb:
--------------------------------------------------------------------------------
1 | require 'rails_helper'
2 |
3 | # test `` component
4 | # test `Img.assetPath()`
5 | # test `function imageTag()`
6 | # test images in multi level directories
7 |
8 | describe 'js features', type: :feature, js: true do
9 | before :each do
10 | visit '/posts/show'
11 | end
12 |
13 | # disable WARNING: The next major version of capybara-webkit will require at least version 5.0 of Qt. You're using version 4.8.6.
14 | # make sure capybara-webkit is working well first
15 | # refs:
16 | # https://github.com/thoughtbot/capybara-webkit/issues/680 # Doesn't see dynamic content (React.js)
17 | # https://github.com/thoughtbot/capybara-webkit/issues/885 # Support for QT 5.6
18 | # https://github.com/thoughtbot/capybara-webkit/wiki/Installing-Qt-and-compiling-capybara-webkit
19 | # http://stackoverflow.com/questions/17075380/can-i-use-homebrews-qt5-with-capybara-webkit
20 | it "react_ujs is working" do
21 | expect_no_js_errors
22 | expect(page).to have_content 'Title: Demo for gem react-rails-img'
23 | end
24 |
25 | it "component should be rendered as " do
26 | expect(page).to have_content 'rails logo(100)'
27 | expect(page).to have_selector 'img[width="100"][src="/assets/rails-logo.svg"]'
28 | end
29 |
30 | it "Img.assetPath()" do
31 | expect(page).to have_content 'rails logo(50)'
32 | expect(page).to have_selector 'img[width="50"][src="/assets/rails-logo.svg"]'
33 | end
34 |
35 | it "function imageTag()" do
36 | expect(page).to have_content 'rails logo(25)'
37 | expect(page).to have_selector 'img[width="25"][src="/assets/rails-logo.svg"]'
38 | end
39 |
40 | it "images in multi level directories" do
41 | expect(page).to have_content 'reactjs logo'
42 | expect(page).to have_selector 'img[class="logo"][src="/assets/logo/reactjs-logo.svg"]'
43 | end
44 |
45 | context "when run in production/staging env" do
46 | around do |example|
47 | Rails.env = 'staging' # simulate running in staging env
48 | example.run
49 | Rails.env = 'test'
50 | end
51 |
52 | it "should compute assets path" do
53 | visit '/posts/show?debug_assets=true' # passing debug_assets=true to expand assets links
54 |
55 | expect(page).to have_content 'rails logo(100)'
56 | expect(page).to have_selector 'img[width="100"][src="/assets/rails-logo-e6d7efc676da55da20de9da9e4d231a44542d457e0191a3a1074a945bb194363.svg"]'
57 | end
58 | end
59 |
60 | it "allow to use custom props with component " do
61 | expect(page).to have_content 'custom props with component:'
62 | expect(page).to have_selector 'img[id="img-custom-props"][class="logo"][src="/assets/logo/reactjs-logo.svg"][alt="logo/reactjs-logo.svg"][data-foo="bar"][data-clicked="false"]'
63 | page.find_by_id('img-custom-props').trigger(:click)
64 | expect(page).to have_selector 'img[id="img-custom-props"][class="logo"][src="/assets/logo/reactjs-logo.svg"][alt="logo/reactjs-logo.svg"][data-foo="bar"][data-clicked="true"]'
65 | end
66 |
67 | it "allow to use custom props with imageTag()" do
68 | expect(page).to have_content 'custom props with helper:'
69 | expect(page).to have_selector 'img[id="img-custom-props-helper"][class="logo"][src="/assets/logo/reactjs-logo.svg"][alt="logo/reactjs-logo.svg"][data-foo="bar"][data-clicked="false"]'
70 | page.find_by_id('img-custom-props-helper').trigger(:click)
71 | expect(page).to have_selector 'img[id="img-custom-props-helper"][class="logo"][src="/assets/logo/reactjs-logo.svg"][alt="logo/reactjs-logo.svg"][data-foo="bar"][data-clicked="true"]'
72 | end
73 | end
74 |
--------------------------------------------------------------------------------
/spec/dummy/config/environments/production.rb:
--------------------------------------------------------------------------------
1 | Rails.application.configure do
2 | # Settings specified here will take precedence over those in config/application.rb.
3 |
4 | # Code is not reloaded between requests.
5 | config.cache_classes = true
6 |
7 | # Eager load code on boot. This eager loads most of Rails and
8 | # your application in memory, allowing both threaded web servers
9 | # and those relying on copy on write to perform better.
10 | # Rake tasks automatically ignore this option for performance.
11 | config.eager_load = true
12 |
13 | # Full error reports are disabled and caching is turned on.
14 | config.consider_all_requests_local = false
15 | config.action_controller.perform_caching = true
16 |
17 | # Enable Rack::Cache to put a simple HTTP cache in front of your application
18 | # Add `rack-cache` to your Gemfile before enabling this.
19 | # For large-scale production use, consider using a caching reverse proxy like
20 | # NGINX, varnish or squid.
21 | # config.action_dispatch.rack_cache = true
22 |
23 | # Disable serving static files from the `/public` folder by default since
24 | # Apache or NGINX already handles this.
25 | config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
26 |
27 | # Compress JavaScripts and CSS.
28 | config.assets.js_compressor = :uglifier
29 | # config.assets.css_compressor = :sass
30 |
31 | # Do not fallback to assets pipeline if a precompiled asset is missed.
32 | config.assets.compile = false
33 |
34 | # Asset digests allow you to set far-future HTTP expiration dates on all assets,
35 | # yet still be able to expire them through the digest params.
36 | config.assets.digest = true
37 |
38 | # `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb
39 |
40 | # Specifies the header that your server uses for sending files.
41 | # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache
42 | # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX
43 |
44 | # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
45 | # config.force_ssl = true
46 |
47 | # Use the lowest log level to ensure availability of diagnostic information
48 | # when problems arise.
49 | config.log_level = :debug
50 |
51 | # Prepend all log lines with the following tags.
52 | # config.log_tags = [ :subdomain, :uuid ]
53 |
54 | # Use a different logger for distributed setups.
55 | # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
56 |
57 | # Use a different cache store in production.
58 | # config.cache_store = :mem_cache_store
59 |
60 | # Enable serving of images, stylesheets, and JavaScripts from an asset server.
61 | # config.action_controller.asset_host = 'http://assets.example.com'
62 |
63 | # Ignore bad email addresses and do not raise email delivery errors.
64 | # Set this to true and configure the email server for immediate delivery to raise delivery errors.
65 | # config.action_mailer.raise_delivery_errors = false
66 |
67 | # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
68 | # the I18n.default_locale when a translation cannot be found).
69 | config.i18n.fallbacks = true
70 |
71 | # Send deprecation notices to registered listeners.
72 | config.active_support.deprecation = :notify
73 |
74 | # Use default logging formatter so that PID and timestamp are not suppressed.
75 | config.log_formatter = ::Logger::Formatter.new
76 |
77 | # Do not dump schema after migrations.
78 | config.active_record.dump_schema_after_migration = false
79 | end
80 |
--------------------------------------------------------------------------------