diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c41f19b --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +.bundle/ +log/*.log +pkg/ +.idea/ +spec/dummy/db/*.sqlite3 +spec/dummy/db/*.sqlite3-journal +spec/dummy/log/*.log +spec/dummy/tmp/ +spec/dummy/.sass-cache diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..091e398 --- /dev/null +++ b/.rspec @@ -0,0 +1,3 @@ +--color +--require rails_helper +--require spec_helper diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..5ea7764 --- /dev/null +++ b/Gemfile @@ -0,0 +1,11 @@ +source 'https://rubygems.org' + +gem 'rails' +gem 'jquery-rails' + +group :development, :test do + gem 'rspec-rails', '~> 3.0' + gem 'factory_girl_rails' + gem 'sqlite3' + gem 'database_cleaner' +end diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..1f1278e --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,137 @@ +GEM + remote: https://rubygems.org/ + specs: + actionmailer (4.2.0) + actionpack (= 4.2.0) + actionview (= 4.2.0) + activejob (= 4.2.0) + mail (~> 2.5, >= 2.5.4) + rails-dom-testing (~> 1.0, >= 1.0.5) + actionpack (4.2.0) + actionview (= 4.2.0) + activesupport (= 4.2.0) + rack (~> 1.6.0) + rack-test (~> 0.6.2) + rails-dom-testing (~> 1.0, >= 1.0.5) + rails-html-sanitizer (~> 1.0, >= 1.0.1) + actionview (4.2.0) + activesupport (= 4.2.0) + builder (~> 3.1) + erubis (~> 2.7.0) + rails-dom-testing (~> 1.0, >= 1.0.5) + rails-html-sanitizer (~> 1.0, >= 1.0.1) + activejob (4.2.0) + activesupport (= 4.2.0) + globalid (>= 0.3.0) + activemodel (4.2.0) + activesupport (= 4.2.0) + builder (~> 3.1) + activerecord (4.2.0) + activemodel (= 4.2.0) + activesupport (= 4.2.0) + arel (~> 6.0) + activesupport (4.2.0) + i18n (~> 0.7) + json (~> 1.7, >= 1.7.7) + minitest (~> 5.1) + thread_safe (~> 0.3, >= 0.3.4) + tzinfo (~> 1.1) + arel (6.0.0) + builder (3.2.2) + database_cleaner (1.4.0) + diff-lcs (1.2.5) + erubis (2.7.0) + factory_girl (4.5.0) + activesupport (>= 3.0.0) + factory_girl_rails (4.5.0) + factory_girl (~> 4.5.0) + railties (>= 3.0.0) + globalid (0.3.0) + activesupport (>= 4.1.0) + hike (1.2.3) + i18n (0.7.0) + jquery-rails (4.0.3) + rails-dom-testing (~> 1.0) + railties (>= 4.2.0) + thor (>= 0.14, < 2.0) + json (1.8.2) + loofah (2.0.1) + nokogiri (>= 1.5.9) + mail (2.6.3) + mime-types (>= 1.16, < 3) + mime-types (2.4.3) + mini_portile (0.6.2) + minitest (5.5.1) + multi_json (1.10.1) + nokogiri (1.6.6.2) + mini_portile (~> 0.6.0) + rack (1.6.0) + rack-test (0.6.3) + rack (>= 1.0) + rails (4.2.0) + actionmailer (= 4.2.0) + actionpack (= 4.2.0) + actionview (= 4.2.0) + activejob (= 4.2.0) + activemodel (= 4.2.0) + activerecord (= 4.2.0) + activesupport (= 4.2.0) + bundler (>= 1.3.0, < 2.0) + railties (= 4.2.0) + sprockets-rails + rails-deprecated_sanitizer (1.0.3) + activesupport (>= 4.2.0.alpha) + rails-dom-testing (1.0.5) + activesupport (>= 4.2.0.beta, < 5.0) + nokogiri (~> 1.6.0) + rails-deprecated_sanitizer (>= 1.0.1) + rails-html-sanitizer (1.0.1) + loofah (~> 2.0) + railties (4.2.0) + actionpack (= 4.2.0) + activesupport (= 4.2.0) + rake (>= 0.8.7) + thor (>= 0.18.1, < 2.0) + rake (10.4.2) + rspec-core (3.1.7) + rspec-support (~> 3.1.0) + rspec-expectations (3.1.2) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.1.0) + rspec-mocks (3.1.3) + rspec-support (~> 3.1.0) + rspec-rails (3.1.0) + actionpack (>= 3.0) + activesupport (>= 3.0) + railties (>= 3.0) + rspec-core (~> 3.1.0) + rspec-expectations (~> 3.1.0) + rspec-mocks (~> 3.1.0) + rspec-support (~> 3.1.0) + rspec-support (3.1.2) + sprockets (2.12.3) + hike (~> 1.2) + multi_json (~> 1.0) + rack (~> 1.0) + tilt (~> 1.1, != 1.3.0) + sprockets-rails (2.2.4) + actionpack (>= 3.0) + activesupport (>= 3.0) + sprockets (>= 2.8, < 4.0) + sqlite3 (1.3.10) + thor (0.19.1) + thread_safe (0.3.4) + tilt (1.4.1) + tzinfo (1.2.2) + thread_safe (~> 0.1) + +PLATFORMS + ruby + +DEPENDENCIES + database_cleaner + factory_girl_rails + jquery-rails + rails + rspec-rails (~> 3.0) + sqlite3 diff --git a/MIT-LICENSE b/MIT-LICENSE new file mode 100644 index 0000000..c08b411 --- /dev/null +++ b/MIT-LICENSE @@ -0,0 +1,20 @@ +Copyright 2015 Alexey + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index 8d1c8b6..5a3eeac 100644 --- a/README.md +++ b/README.md @@ -1 +1,79 @@ - +[ckeditor]: https://github.com/galetahub/ckeditor + +SimpleEditable +=========================== +This gem allow you to edit simple html text and object fields inline, on click. + +```ruby +# Gemfile + +gem 'simple_editable', :git => 'git@github.com:Rezonans/simple-editable.git' +``` +Install it with: + +```bash +rails g simple_editable:install +``` + +Then run `rake db:migrate` + +### Allow your views to use simple-editable helpers: + +```ruby +class ApplicationController < ActionController::Base + helper SimpleEditable::ApplicationHelper +end + +``` + +Configuration +------------- +After installing you get initialize file: `config/initializers/simple_editable.rb` +Edit it according to your settings + +```ruby +# This setting changes the method which Simple Editable calls +# (within the application helper) to return the user who can access to inline editing. +# +# If false set, it allow anybody to use it + +SimpleEditable.admin_user = false + +# Simple Editable will automatically call an authentication +# method in a before filter of all controller actions to +# ensure that there is a currently logged in admin user. +# +# This setting changes the method which Simple Editable calls +# within the application controller. +# +# If false set, authentication method will not be called + +SimpleEditable.authentication_method = false +``` + +Now you can use it with two helpers +`inline_object` and `inline_text` + +Examples of usage +------------- + +```ruby +#inline_object(tag, object, attribute, options = {}) +inline_object(:div, obj, :field, class: 'hello-class another-class') + +#inline_text(tag, key, default_value, options = {}) +inline_text(:div, 'pages.home.title', 'Hello World', class: 'hello-class another-class', position: 'right') + +#Allowed values for :position in %w(left, right, top, bottom) + +``` + +When you сlick on the appropriate tag, you can edit it, with html attribute `contenteditable = true` and when focus is lost, data will send and saved. + +Customizing +------------- +You can change the default behavior of editing with editing file `app/assets/javascripts/simple_editable/init.js` which generates on installing gem. + +Also you can use any wysiwyg editor, which has the ability to apply editor inline (on tags with `contenteditable = true`). + +One of them is [CKEditor][ckeditor]. Just install it by documentation. Inline Editing is enabled directly in it on HTML elements through the HTML5 contenteditable attribute. diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..236bf2c --- /dev/null +++ b/Rakefile @@ -0,0 +1,36 @@ +begin + require 'bundler/setup' +rescue LoadError + puts 'You must `gem install bundler` and `bundle install` to run rake tasks' +end + +require 'rdoc/task' + +RDoc::Task.new(:rdoc) do |rdoc| + rdoc.rdoc_dir = 'rdoc' + rdoc.title = 'SimpleEditable' + rdoc.options << '--line-numbers' + rdoc.rdoc_files.include('README.rdoc') + rdoc.rdoc_files.include('lib/**/*.rb') +end + +APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__) +load 'rails/tasks/engine.rake' + + +load 'rails/tasks/statistics.rake' + + + +Bundler::GemHelper.install_tasks + +require 'rake/testtask' + +Rake::TestTask.new(:test) do |t| + t.libs << 'lib' + t.libs << 'test' + t.pattern = 'spec/**/*_test.rb' + t.verbose = false +end + +task default: :test diff --git a/app/controllers/simple_editable/application_controller.rb b/app/controllers/simple_editable/application_controller.rb new file mode 100644 index 0000000..0e88a36 --- /dev/null +++ b/app/controllers/simple_editable/application_controller.rb @@ -0,0 +1,11 @@ +module SimpleEditable + class ApplicationController < ActionController::Base + before_filter :simple_editable_authentication_method + + private + + def simple_editable_authentication_method + SimpleEditable.authentication_method ? send(SimpleEditable.authentication_method) : true + end + end +end diff --git a/app/controllers/simple_editable/inline_objects_controller.rb b/app/controllers/simple_editable/inline_objects_controller.rb new file mode 100644 index 0000000..2b76bba --- /dev/null +++ b/app/controllers/simple_editable/inline_objects_controller.rb @@ -0,0 +1,11 @@ +module SimpleEditable + class InlineObjectsController < ApplicationController + def update + klass = params[:class_name].constantize + object = klass.find(params[:id]) + object.send(:write_attribute, params[:attribute], params[:value]) + object.save(validate: false) + head :ok + end + end +end diff --git a/app/controllers/simple_editable/inline_texts_controller.rb b/app/controllers/simple_editable/inline_texts_controller.rb new file mode 100644 index 0000000..49d739a --- /dev/null +++ b/app/controllers/simple_editable/inline_texts_controller.rb @@ -0,0 +1,9 @@ +module SimpleEditable + class InlineTextsController < ApplicationController + def update + value = params[:text_only] == 'true' ? ActionController::Base.helpers.sanitize(params[:value], tags: ['br']) : params[:value] + SimpleEditable::InlineTextStorage.new.update(params[:pk], value) + head :ok + end + end +end diff --git a/app/helpers/simple_editable/application_helper.rb b/app/helpers/simple_editable/application_helper.rb new file mode 100644 index 0000000..11db134 --- /dev/null +++ b/app/helpers/simple_editable/application_helper.rb @@ -0,0 +1,60 @@ +module SimpleEditable + module ApplicationHelper + # Helper method for inline editing + # Example of Usage: + # = inline_text(:div, 'pages.home.title', 'Hello World', class: 'hello-class another-class', position: 'right') + # Allowed values for :position in %w(left, right, top, bottom) + def inline_text(tag, key, default_value, options = {}) + @storage ||= InlineTextStorage.new + if simple_editable_admin_user.present? + opts = { + 'contenteditable' => 'true', + 'object' => 'false', + 'data-text-only' => options[:text_only], + 'data-pk' => key, + 'data-url' => simple_editable.update_inline_text_path, + 'class' => options[:class] + } + + opts['data-placement'] = options[:position] if options[:position] + + content_tag(tag, opts) do + raw(@storage.find_or_create(key, default_value)) + end + else + content_tag(tag, class: "#{options[:class]}") do + raw(@storage.find_or_create(key, default_value)) + end + end + end + + def inline_object(tag, object, attribute, options = {}) + if simple_editable_admin_user.present? + opts = { + 'contenteditable' => 'true', + 'object' => 'true', + 'data-attribute' => attribute, + 'data-id' => object.id, + 'data-class' => object.class.to_s, + 'data-url' => simple_editable.update_inline_object_path, + 'class' => options[:class], + 'data-text-only' => options[:text_only], + } + + opts['data-placement'] = options[:position] if options[:position] + + content_tag(tag, opts) do + raw(object.send(attribute)) + end + else + content_tag(tag, class: "#{options[:class]}") do + raw(object.send(attribute)) + end + end + end + + def simple_editable_admin_user + SimpleEditable.admin_user ? send(SimpleEditable.admin_user) : true + end + end +end diff --git a/app/models/simple_editable/inline_text.rb b/app/models/simple_editable/inline_text.rb new file mode 100644 index 0000000..270c173 --- /dev/null +++ b/app/models/simple_editable/inline_text.rb @@ -0,0 +1,7 @@ +module SimpleEditable + class InlineText < ActiveRecord::Base + self.table_name = 'simple_editable_inline_texts' + self.primary_key = :key + validates :key, :text, presence: true + end +end diff --git a/app/models/simple_editable/inline_text_storage.rb b/app/models/simple_editable/inline_text_storage.rb new file mode 100644 index 0000000..ef4ba0f --- /dev/null +++ b/app/models/simple_editable/inline_text_storage.rb @@ -0,0 +1,70 @@ +module SimpleEditable + class InlineTextStorage + TIMESTAMP_KEY = 'inline_texts_timestamp' + TEXTS_KEY = 'inline_texts' + @@timestamp = nil + @@texts = {} + + def initialize + if expired? + read_all + else + data = Rails.cache.read(TEXTS_KEY) + @@texts = data.nil? ? {} : Marshal.load(data) + end + end + + def read_all + @@texts = InlineText.all.inject({}) do |result, inline_text| + result[inline_text.key] = inline_text.text + result + end + update_cache + end + + # drops entire database for inline texts + def drop_all + InlineText.delete_all + Rails.cache.write(TIMESTAMP_KEY, nil) + Rails.cache.write(TEXTS_KEY, nil) + end + + def find_or_create(key, text) + if !@@texts.has_key?(key) + inline_text = InlineText.find_by_key(key) + if inline_text.nil? + InlineText.create(key: key, text: text) + else + inline_text.text = text + inline_text.save + end + @@texts[key] = text + update_cache + @@texts[key] + else + @@texts[key] + end + end + + def update(key, value) + inline_text = InlineText.find_by_key!(key) + inline_text.text = value + inline_text.save + @@texts[key] = value + update_cache + end + + private + + def update_cache + Rails.cache.write(TEXTS_KEY, Marshal.dump(@@texts)) + @@timestamp = Time.now.to_i + Rails.cache.write(TIMESTAMP_KEY, @@timestamp) + end + + def expired? + timestamp = Rails.cache.read(TIMESTAMP_KEY) + @@timestamp != timestamp + end + end +end diff --git a/bin/rails b/bin/rails new file mode 100755 index 0000000..21509ab --- /dev/null +++ b/bin/rails @@ -0,0 +1,12 @@ +#!/usr/bin/env ruby +# This command will automatically be run when you run "rails" with Rails 4 gems installed from the root of your application. + +ENGINE_ROOT = File.expand_path('../..', __FILE__) +ENGINE_PATH = File.expand_path('../../lib/simple_editable/engine', __FILE__) + +# Set up gems listed in the Gemfile. +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) +require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE']) + +require 'rails/all' +require 'rails/engine/commands' diff --git a/config/initializers/inline_texts.rb b/config/initializers/inline_texts.rb new file mode 100644 index 0000000..21a8d82 --- /dev/null +++ b/config/initializers/inline_texts.rb @@ -0,0 +1,5 @@ +begin + SimpleEditable::InlineTextStorage.new.read_all +rescue => e + puts 'was not able to load inline texts storage' +end diff --git a/config/routes.rb b/config/routes.rb new file mode 100644 index 0000000..091381c --- /dev/null +++ b/config/routes.rb @@ -0,0 +1,4 @@ +SimpleEditable::Engine.routes.draw do + post 'update_inline_text' => 'inline_texts#update', as: :update_inline_text + post 'update_inline_object' => 'inline_objects#update', as: :update_inline_object +end diff --git a/db/migrate/20150130104824_create_simple_editable_inline_texts.rb b/db/migrate/20150130104824_create_simple_editable_inline_texts.rb new file mode 100644 index 0000000..4a9605a --- /dev/null +++ b/db/migrate/20150130104824_create_simple_editable_inline_texts.rb @@ -0,0 +1,13 @@ +class CreateSimpleEditableInlineTexts < ActiveRecord::Migration + def up + create_table :simple_editable_inline_texts, id: false do |t| + t.string :key + t.text :text + end + add_index :simple_editable_inline_texts, :key, unique: true + end + + def down + drop_table :simple_editable_inline_texts + end +end diff --git a/lib/generators/simple_editable/install_generator.rb b/lib/generators/simple_editable/install_generator.rb new file mode 100644 index 0000000..06c9a4c --- /dev/null +++ b/lib/generators/simple_editable/install_generator.rb @@ -0,0 +1,25 @@ +require 'rails/generators/active_record' + +module SimpleEditable + class InstallGenerator < ActiveRecord::Generators::Base + desc 'This installs simple editable gem with initializers and migrations' + source_root File.expand_path("../templates", __FILE__) + argument :name, type: :string, default: 'simple_editable' + + def create_initializer_file + copy_file 'simple_editable.rb', 'config/initializers/simple_editable.rb' + end + + def create_migrations + migration_template 'migrations/create_simple_editable_inline_texts.rb', 'db/migrate/create_simple_editable_inline_texts.rb' + end + + def install_assets + template 'assets/init.js', 'app/assets/javascripts/simple_editable/init.js' + end + + def setup_routes + route 'mount SimpleEditable::Engine => "/simple_editable", as: "simple_editable"' + end + end +end diff --git a/lib/generators/simple_editable/templates/assets/init.js b/lib/generators/simple_editable/templates/assets/init.js new file mode 100644 index 0000000..47a39de --- /dev/null +++ b/lib/generators/simple_editable/templates/assets/init.js @@ -0,0 +1,33 @@ +$(document).ready(function() { + $('body, html').on('focusout', '[contenteditable=true][object=false], [contenteditable=true][object=true]', function (e) { + var $input = $(e.target), + url = $input.attr('data-url'), + object = $input.attr('object'); + + if (object == 'true' ) { + var id = $input.attr('data-id'), + class_name = $input.attr('data-class'), + attribute = $input.attr('data-attribute'), + data = { + id: id, + class_name: class_name, + attribute: attribute, + value: $input.html() + }; + + } else if (object == 'false') { + var key = $input.attr('data-pk'), + data = { + text_only: $input.data('data-text-only'), + value: $input.html(), + pk: key + }; + } + + $.ajax({ + method: 'POST', + url: url, + data: data + }); + }); +}); diff --git a/lib/generators/simple_editable/templates/migrations/create_simple_editable_inline_texts.rb b/lib/generators/simple_editable/templates/migrations/create_simple_editable_inline_texts.rb new file mode 100644 index 0000000..4a9605a --- /dev/null +++ b/lib/generators/simple_editable/templates/migrations/create_simple_editable_inline_texts.rb @@ -0,0 +1,13 @@ +class CreateSimpleEditableInlineTexts < ActiveRecord::Migration + def up + create_table :simple_editable_inline_texts, id: false do |t| + t.string :key + t.text :text + end + add_index :simple_editable_inline_texts, :key, unique: true + end + + def down + drop_table :simple_editable_inline_texts + end +end diff --git a/lib/generators/simple_editable/templates/simple_editable.rb b/lib/generators/simple_editable/templates/simple_editable.rb new file mode 100644 index 0000000..f1731b2 --- /dev/null +++ b/lib/generators/simple_editable/templates/simple_editable.rb @@ -0,0 +1,17 @@ +# This setting changes the method which Simple Editable calls +# (within the application helper) to return the user who can access to inline editing. +# +# If false set, it allow anybody to use it + +SimpleEditable.admin_user = false + +# Simple Editable will automatically call an authentication +# method in a before filter of all controller actions to +# ensure that there is a currently logged in admin user. +# +# This setting changes the method which Simple Editable calls +# within the application controller. +# +# If false set, authentication method will not be called + +SimpleEditable.authentication_method = false diff --git a/lib/simple_editable.rb b/lib/simple_editable.rb new file mode 100644 index 0000000..61b08e4 --- /dev/null +++ b/lib/simple_editable.rb @@ -0,0 +1,6 @@ +require "simple_editable/engine" + +module SimpleEditable + mattr_accessor :admin_user + mattr_accessor :authentication_method +end diff --git a/lib/simple_editable/engine.rb b/lib/simple_editable/engine.rb new file mode 100644 index 0000000..b7b70e3 --- /dev/null +++ b/lib/simple_editable/engine.rb @@ -0,0 +1,9 @@ +module SimpleEditable + class Engine < ::Rails::Engine + isolate_namespace SimpleEditable + + config.generators do |g| + g.test_framework :rspec + end + end +end diff --git a/lib/simple_editable/version.rb b/lib/simple_editable/version.rb new file mode 100644 index 0000000..4c1a8d5 --- /dev/null +++ b/lib/simple_editable/version.rb @@ -0,0 +1,3 @@ +module SimpleEditable + VERSION = "0.0.1" +end diff --git a/simple_editable.gemspec b/simple_editable.gemspec new file mode 100644 index 0000000..64fd117 --- /dev/null +++ b/simple_editable.gemspec @@ -0,0 +1,25 @@ +$:.push File.expand_path("../lib", __FILE__) + +# Maintain your gem's version: +require "simple_editable/version" + +# Describe your gem and declare its dependencies: +Gem::Specification.new do |s| + s.name = "simple_editable" + s.version = SimpleEditable::VERSION + s.authors = ["Alexey"] + s.email = ["mail@brocoders.com"] + s.homepage = "http://brocoders.com" + s.summary = "Quick inline editing gem" + s.description = "This gem makes possible to edit objects and plain text in one click" + s.license = "MIT" + + s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"] + s.test_files = Dir["spec/**/*"] + + s.add_dependency 'rails', '>= 4.0', '<= 4.2' + + s.add_dependency 'jquery-rails' + + s.add_development_dependency "sqlite3" +end diff --git a/spec/controllers/simple_editable/inline_objects_controller_spec.rb b/spec/controllers/simple_editable/inline_objects_controller_spec.rb new file mode 100644 index 0000000..4d8626d --- /dev/null +++ b/spec/controllers/simple_editable/inline_objects_controller_spec.rb @@ -0,0 +1,30 @@ +require 'rails_helper' + +module SimpleEditable + RSpec.describe InlineObjectsController, type: :controller do + before :all do + m = ActiveRecord::Migration.new + m.verbose = false + m.create_table :simple_editable_test_table do |t| + t.string :name + end + end + + after :all do + m = ActiveRecord::Migration.new + m.verbose = false + m.drop_table :simple_editable_test_table + end + + describe 'POST #update' do + routes { SimpleEditable::Engine.routes } + it 'should update a record' do + test = Testing.create(name: 'Name') + put :update, {class_name: test.class, id: test.id, attribute: :name, value: 'New name' } + test.reload + + expect(test.name).to eq('New name') + end + end + end +end diff --git a/spec/controllers/simple_editable/inline_texts_controller_spec.rb b/spec/controllers/simple_editable/inline_texts_controller_spec.rb new file mode 100644 index 0000000..6773550 --- /dev/null +++ b/spec/controllers/simple_editable/inline_texts_controller_spec.rb @@ -0,0 +1,16 @@ +require 'rails_helper' + +module SimpleEditable + RSpec.describe InlineTextsController, type: :controller do + describe 'GET #update' do + routes { SimpleEditable::Engine.routes } + it 'should update inline text' do + inline_text = InlineText.create!(key: 'test', text: 'Hello, this is test') + put :update, pk: inline_text.key, value: 'New Test' + expect(response).to be_successful + inline_text.reload + expect(inline_text.text).to eq('New Test') + end + end + end +end diff --git a/spec/dummy/Rakefile b/spec/dummy/Rakefile new file mode 100644 index 0000000..ba6b733 --- /dev/null +++ b/spec/dummy/Rakefile @@ -0,0 +1,6 @@ +# Add your own tasks in files placed in lib/tasks ending in .rake, +# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. + +require File.expand_path('../config/application', __FILE__) + +Rails.application.load_tasks diff --git a/spec/dummy/app/controllers/application_controller.rb b/spec/dummy/app/controllers/application_controller.rb new file mode 100644 index 0000000..d83690e --- /dev/null +++ b/spec/dummy/app/controllers/application_controller.rb @@ -0,0 +1,5 @@ +class ApplicationController < ActionController::Base + # Prevent CSRF attacks by raising an exception. + # For APIs, you may want to use :null_session instead. + protect_from_forgery with: :exception +end diff --git a/spec/dummy/app/models/testing.rb b/spec/dummy/app/models/testing.rb new file mode 100644 index 0000000..c447871 --- /dev/null +++ b/spec/dummy/app/models/testing.rb @@ -0,0 +1,3 @@ +class Testing < ActiveRecord::Base + self.table_name = 'simple_editable_test_table' +end diff --git a/spec/dummy/bin/bundle b/spec/dummy/bin/bundle new file mode 100755 index 0000000..66e9889 --- /dev/null +++ b/spec/dummy/bin/bundle @@ -0,0 +1,3 @@ +#!/usr/bin/env ruby +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) +load Gem.bin_path('bundler', 'bundle') diff --git a/spec/dummy/bin/rails b/spec/dummy/bin/rails new file mode 100755 index 0000000..5191e69 --- /dev/null +++ b/spec/dummy/bin/rails @@ -0,0 +1,4 @@ +#!/usr/bin/env ruby +APP_PATH = File.expand_path('../../config/application', __FILE__) +require_relative '../config/boot' +require 'rails/commands' diff --git a/spec/dummy/bin/rake b/spec/dummy/bin/rake new file mode 100755 index 0000000..1724048 --- /dev/null +++ b/spec/dummy/bin/rake @@ -0,0 +1,4 @@ +#!/usr/bin/env ruby +require_relative '../config/boot' +require 'rake' +Rake.application.run diff --git a/spec/dummy/bin/setup b/spec/dummy/bin/setup new file mode 100755 index 0000000..acdb2c1 --- /dev/null +++ b/spec/dummy/bin/setup @@ -0,0 +1,29 @@ +#!/usr/bin/env ruby +require 'pathname' + +# path to your application root. +APP_ROOT = Pathname.new File.expand_path('../../', __FILE__) + +Dir.chdir APP_ROOT do + # This script is a starting point to setup your application. + # Add necessary setup steps to this file: + + puts "== Installing dependencies ==" + system "gem install bundler --conservative" + system "bundle check || bundle install" + + # puts "\n== Copying sample files ==" + # unless File.exist?("config/database.yml") + # system "cp config/database.yml.sample config/database.yml" + # end + + puts "\n== Preparing database ==" + system "bin/rake db:setup" + + puts "\n== Removing old logs and tempfiles ==" + system "rm -f log/*" + system "rm -rf tmp/cache" + + puts "\n== Restarting application server ==" + system "touch tmp/restart.txt" +end diff --git a/spec/dummy/config.ru b/spec/dummy/config.ru new file mode 100644 index 0000000..bd83b25 --- /dev/null +++ b/spec/dummy/config.ru @@ -0,0 +1,4 @@ +# This file is used by Rack-based servers to start the application. + +require ::File.expand_path('../config/environment', __FILE__) +run Rails.application diff --git a/spec/dummy/config/application.rb b/spec/dummy/config/application.rb new file mode 100644 index 0000000..9738f56 --- /dev/null +++ b/spec/dummy/config/application.rb @@ -0,0 +1,32 @@ +require File.expand_path('../boot', __FILE__) + +# Pick the frameworks you want: +require "active_record/railtie" +require "action_controller/railtie" +require "action_mailer/railtie" +require "action_view/railtie" +require "sprockets/railtie" +# require "rails/test_unit/railtie" + +Bundler.require(*Rails.groups) +require "simple_editable" + +module Dummy + class Application < Rails::Application + # Settings in config/environments/* take precedence over those specified here. + # Application configuration should go into files in config/initializers + # -- all .rb files in that directory are automatically loaded. + + # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. + # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. + # config.time_zone = 'Central Time (US & Canada)' + + # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. + # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] + # config.i18n.default_locale = :de + + # Do not swallow errors in after_commit/after_rollback callbacks. + config.active_record.raise_in_transactional_callbacks = true + end +end + diff --git a/spec/dummy/config/boot.rb b/spec/dummy/config/boot.rb new file mode 100644 index 0000000..6266cfc --- /dev/null +++ b/spec/dummy/config/boot.rb @@ -0,0 +1,5 @@ +# Set up gems listed in the Gemfile. +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__) + +require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE']) +$LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__) diff --git a/spec/dummy/config/database.yml b/spec/dummy/config/database.yml new file mode 100644 index 0000000..1c1a37c --- /dev/null +++ b/spec/dummy/config/database.yml @@ -0,0 +1,25 @@ +# SQLite version 3.x +# gem install sqlite3 +# +# Ensure the SQLite 3 gem is defined in your Gemfile +# gem 'sqlite3' +# +default: &default + adapter: sqlite3 + pool: 5 + timeout: 5000 + +development: + <<: *default + database: db/development.sqlite3 + +# Warning: The database defined as "test" will be erased and +# re-generated from your development database when you run "rake". +# Do not set this db to the same as development or production. +test: + <<: *default + database: db/test.sqlite3 + +production: + <<: *default + database: db/production.sqlite3 diff --git a/spec/dummy/config/environment.rb b/spec/dummy/config/environment.rb new file mode 100644 index 0000000..ee8d90d --- /dev/null +++ b/spec/dummy/config/environment.rb @@ -0,0 +1,5 @@ +# Load the Rails application. +require File.expand_path('../application', __FILE__) + +# Initialize the Rails application. +Rails.application.initialize! diff --git a/spec/dummy/config/environments/development.rb b/spec/dummy/config/environments/development.rb new file mode 100644 index 0000000..b55e214 --- /dev/null +++ b/spec/dummy/config/environments/development.rb @@ -0,0 +1,41 @@ +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + # In the development environment your application's code is reloaded on + # every request. This slows down response time but is perfect for development + # since you don't have to restart the web server when you make code changes. + config.cache_classes = false + + # Do not eager load code on boot. + config.eager_load = false + + # Show full error reports and disable caching. + config.consider_all_requests_local = true + config.action_controller.perform_caching = false + + # Don't care if the mailer can't send. + config.action_mailer.raise_delivery_errors = false + + # Print deprecation notices to the Rails logger. + config.active_support.deprecation = :log + + # Raise an error on page load if there are pending migrations. + config.active_record.migration_error = :page_load + + # Debug mode disables concatenation and preprocessing of assets. + # This option may cause significant delays in view rendering with a large + # number of complex assets. + config.assets.debug = true + + # Asset digests allow you to set far-future HTTP expiration dates on all assets, + # yet still be able to expire them through the digest params. + config.assets.digest = true + + # Adds additional error checking when serving assets at runtime. + # Checks for improperly declared sprockets dependencies. + # Raises helpful error messages. + config.assets.raise_runtime_errors = true + + # Raises error for missing translations + # config.action_view.raise_on_missing_translations = true +end diff --git a/spec/dummy/config/environments/production.rb b/spec/dummy/config/environments/production.rb new file mode 100644 index 0000000..5c1b32e --- /dev/null +++ b/spec/dummy/config/environments/production.rb @@ -0,0 +1,79 @@ +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + # Code is not reloaded between requests. + config.cache_classes = true + + # Eager load code on boot. This eager loads most of Rails and + # your application in memory, allowing both threaded web servers + # and those relying on copy on write to perform better. + # Rake tasks automatically ignore this option for performance. + config.eager_load = true + + # Full error reports are disabled and caching is turned on. + config.consider_all_requests_local = false + config.action_controller.perform_caching = true + + # Enable Rack::Cache to put a simple HTTP cache in front of your application + # Add `rack-cache` to your Gemfile before enabling this. + # For large-scale production use, consider using a caching reverse proxy like + # NGINX, varnish or squid. + # config.action_dispatch.rack_cache = true + + # Disable serving static files from the `/public` folder by default since + # Apache or NGINX already handles this. + config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present? + + # Compress JavaScripts and CSS. + config.assets.js_compressor = :uglifier + # config.assets.css_compressor = :sass + + # Do not fallback to assets pipeline if a precompiled asset is missed. + config.assets.compile = false + + # Asset digests allow you to set far-future HTTP expiration dates on all assets, + # yet still be able to expire them through the digest params. + config.assets.digest = true + + # `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb + + # Specifies the header that your server uses for sending files. + # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache + # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX + + # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. + # config.force_ssl = true + + # Use the lowest log level to ensure availability of diagnostic information + # when problems arise. + config.log_level = :debug + + # Prepend all log lines with the following tags. + # config.log_tags = [ :subdomain, :uuid ] + + # Use a different logger for distributed setups. + # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new) + + # Use a different cache store in production. + # config.cache_store = :mem_cache_store + + # Enable serving of images, stylesheets, and JavaScripts from an asset server. + # config.action_controller.asset_host = 'http://assets.example.com' + + # Ignore bad email addresses and do not raise email delivery errors. + # Set this to true and configure the email server for immediate delivery to raise delivery errors. + # config.action_mailer.raise_delivery_errors = false + + # Enable locale fallbacks for I18n (makes lookups for any locale fall back to + # the I18n.default_locale when a translation cannot be found). + config.i18n.fallbacks = true + + # Send deprecation notices to registered listeners. + config.active_support.deprecation = :notify + + # Use default logging formatter so that PID and timestamp are not suppressed. + config.log_formatter = ::Logger::Formatter.new + + # Do not dump schema after migrations. + config.active_record.dump_schema_after_migration = false +end diff --git a/spec/dummy/config/environments/test.rb b/spec/dummy/config/environments/test.rb new file mode 100644 index 0000000..1c19f08 --- /dev/null +++ b/spec/dummy/config/environments/test.rb @@ -0,0 +1,42 @@ +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + # The test environment is used exclusively to run your application's + # test suite. You never need to work with it otherwise. Remember that + # your test database is "scratch space" for the test suite and is wiped + # and recreated between test runs. Don't rely on the data there! + config.cache_classes = true + + # Do not eager load code on boot. This avoids loading your whole application + # just for the purpose of running a single test. If you are using a tool that + # preloads Rails for running tests, you may have to set it to true. + config.eager_load = false + + # Configure static file server for tests with Cache-Control for performance. + config.serve_static_files = true + config.static_cache_control = 'public, max-age=3600' + + # Show full error reports and disable caching. + config.consider_all_requests_local = true + config.action_controller.perform_caching = false + + # Raise exceptions instead of rendering exception templates. + config.action_dispatch.show_exceptions = false + + # Disable request forgery protection in test environment. + config.action_controller.allow_forgery_protection = false + + # Tell Action Mailer not to deliver emails to the real world. + # The :test delivery method accumulates sent emails in the + # ActionMailer::Base.deliveries array. + config.action_mailer.delivery_method = :test + + # Randomize the order test cases are executed. + config.active_support.test_order = :random + + # Print deprecation notices to the stderr. + config.active_support.deprecation = :stderr + + # Raises error for missing translations + # config.action_view.raise_on_missing_translations = true +end diff --git a/spec/dummy/config/initializers/assets.rb b/spec/dummy/config/initializers/assets.rb new file mode 100644 index 0000000..0d6dbb2 --- /dev/null +++ b/spec/dummy/config/initializers/assets.rb @@ -0,0 +1 @@ +Rails.application.config.assets.version = '1.0' diff --git a/spec/dummy/config/initializers/cookies_serializer.rb b/spec/dummy/config/initializers/cookies_serializer.rb new file mode 100644 index 0000000..7f70458 --- /dev/null +++ b/spec/dummy/config/initializers/cookies_serializer.rb @@ -0,0 +1,3 @@ +# Be sure to restart your server when you modify this file. + +Rails.application.config.action_dispatch.cookies_serializer = :json diff --git a/spec/dummy/config/initializers/filter_parameter_logging.rb b/spec/dummy/config/initializers/filter_parameter_logging.rb new file mode 100644 index 0000000..4a994e1 --- /dev/null +++ b/spec/dummy/config/initializers/filter_parameter_logging.rb @@ -0,0 +1,4 @@ +# Be sure to restart your server when you modify this file. + +# Configure sensitive parameters which will be filtered from the log file. +Rails.application.config.filter_parameters += [:password] diff --git a/spec/dummy/config/initializers/session_store.rb b/spec/dummy/config/initializers/session_store.rb new file mode 100644 index 0000000..e766b67 --- /dev/null +++ b/spec/dummy/config/initializers/session_store.rb @@ -0,0 +1,3 @@ +# Be sure to restart your server when you modify this file. + +Rails.application.config.session_store :cookie_store, key: '_dummy_session' diff --git a/spec/dummy/config/initializers/wrap_parameters.rb b/spec/dummy/config/initializers/wrap_parameters.rb new file mode 100644 index 0000000..e9a25b1 --- /dev/null +++ b/spec/dummy/config/initializers/wrap_parameters.rb @@ -0,0 +1,5 @@ +# Be sure to restart your server when you modify this file. + +ActiveSupport.on_load(:action_controller) do + wrap_parameters format: [:json] if respond_to?(:wrap_parameters) +end diff --git a/spec/dummy/config/routes.rb b/spec/dummy/config/routes.rb new file mode 100644 index 0000000..5837eba --- /dev/null +++ b/spec/dummy/config/routes.rb @@ -0,0 +1,3 @@ +Rails.application.routes.draw do + mount SimpleEditable::Engine => "/simple_editable", as: "simple_editable" +end diff --git a/spec/dummy/config/secrets.yml b/spec/dummy/config/secrets.yml new file mode 100644 index 0000000..32a5fad --- /dev/null +++ b/spec/dummy/config/secrets.yml @@ -0,0 +1,22 @@ +# Be sure to restart your server when you modify this file. + +# Your secret key is used for verifying the integrity of signed cookies. +# If you change this key, all old signed cookies will become invalid! + +# Make sure the secret is at least 30 characters and all random, +# no regular words or you'll be exposed to dictionary attacks. +# You can use `rake secret` to generate a secure secret key. + +# Make sure the secrets in this file are kept private +# if you're sharing your code publicly. + +development: + secret_key_base: d2efa5c206fcf6fae99c16ab002dc76117c9a5bcda6134f1b29440e8a718d1f87001bc96ab938d66524ca506ee1930f226c7319e354349e26ddd94531869bc03 + +test: + secret_key_base: acd5e1955f004fbda4d67793b02e94fb31a70b77c823205a5188efbeba7c49d622089337f051b510afc50b5c58f6df29079391857022f2cb6259374192eb9f77 + +# Do not keep production secrets in the repository, +# instead read values from the environment. +production: + secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> diff --git a/spec/dummy/db/schema.rb b/spec/dummy/db/schema.rb new file mode 100644 index 0000000..5758f1b --- /dev/null +++ b/spec/dummy/db/schema.rb @@ -0,0 +1,23 @@ +# encoding: UTF-8 +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 20150130104824) do + + create_table "simple_editable_inline_texts", id: false, force: :cascade do |t| + t.string "key" + t.text "text" + end + + add_index "simple_editable_inline_texts", ["key"], name: "index_simple_editable_inline_texts_on_key", unique: true + +end diff --git a/spec/dummy/log/.keep b/spec/dummy/log/.keep new file mode 100644 index 0000000..e69de29 diff --git a/spec/helpers/simple_editable/application_helper_helper_spec.rb b/spec/helpers/simple_editable/application_helper_helper_spec.rb new file mode 100644 index 0000000..3f00269 --- /dev/null +++ b/spec/helpers/simple_editable/application_helper_helper_spec.rb @@ -0,0 +1,38 @@ +require 'rails_helper' + +module SimpleEditable + RSpec.describe SimpleEditable::ApplicationHelper, type: :helper do + describe 'inline_text' do + it 'render inline editable tag' do + s_tag = helper.inline_text(:p, 'paragraph', 'This is paragraph', text_only: true, class: 'paragraph') + %w(contenteditable=\"true\" object=\"false\" data-text-only=\"true\" data-url=\"\/simple_editable\/update_inline_text\" class=\"paragraph\" This is paragraph).each do |pattern| + expect(s_tag).to match /#{pattern}/ + end + end + end + + describe 'inline_object' do + it 'render inline editable tag' do + obj = InlineText.create!(key: 'test_title', text: 'Hello, this is title') + s_tag = inline_object(:p, obj, :text, class: 'text') + %w(contenteditable=\"true\" object=\"true\" data-url=\"\/simple_editable\/update_inline_object\" class=\"text\" data-attribute="text").each do |pattern| + expect(s_tag).to match /#{pattern}/ + end + end + end + + describe 'simple_editable_admin_user' do + it 'should return true (no auth method)' do + SimpleEditable.admin_user = false + expect(helper.simple_editable_admin_user).to eq(true) + end + + it 'should return allowed' do + #make a fake auth method + allow(helper).to receive(:auth_method).and_return(:allowed) + SimpleEditable.admin_user = :auth_method + expect(helper.simple_editable_admin_user).to eq(:allowed) + end + end + end +end diff --git a/spec/models/simple_editable/inline_text_spec.rb b/spec/models/simple_editable/inline_text_spec.rb new file mode 100644 index 0000000..70fb523 --- /dev/null +++ b/spec/models/simple_editable/inline_text_spec.rb @@ -0,0 +1,10 @@ +require 'rails_helper' + +module SimpleEditable + RSpec.describe InlineText, type: :model do + it 'should create inline_text' do + text = InlineText.create!(key: 'title', text: 'Hello, this is title') + expect(text).to be_valid + end + end +end diff --git a/spec/models/simple_editable/inline_text_storage_spec.rb b/spec/models/simple_editable/inline_text_storage_spec.rb new file mode 100644 index 0000000..3508633 --- /dev/null +++ b/spec/models/simple_editable/inline_text_storage_spec.rb @@ -0,0 +1,29 @@ +require 'rails_helper' + +module SimpleEditable + RSpec.describe InlineTextStorage, type: :model do + before :all do + @storage = InlineTextStorage.new + @text = InlineText.create!(key: 'title_2', text: 'Hello, this is title') + end + + it 'should read all' do + expect(@storage.read_all).to eq(true) + end + + it 'should drop all' do + @storage.drop_all + expect(InlineText.all.length).to eq(0) + end + + it 'should find record' do + text = @storage.find_or_create('title_2', 'Hello') + expect(text).to eq(@text.text) + end + + it 'should create record' do + text = @storage.find_or_create('title_3', 'Hello') + expect(text).to eq('Hello') + end + end +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb new file mode 100644 index 0000000..51ffae4 --- /dev/null +++ b/spec/rails_helper.rb @@ -0,0 +1,46 @@ +# This file is copied to spec/ when you run 'rails generate rspec:install' +ENV["RAILS_ENV"] ||= 'test' +require 'spec_helper' +require File.expand_path("../dummy/config/environment", __FILE__) +require 'rspec/rails' + +# Add additional requires below this line. Rails is not loaded until this point! + +# Requires supporting ruby files with custom matchers and macros, etc, in +# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are +# run as spec files by default. This means that files in spec/support that end +# in _spec.rb will both be required and run as specs, causing the specs to be +# run twice. It is recommended that you do not name files matching this glob to +# end with _spec.rb. You can configure this pattern with the --pattern +# option on the command line or in ~/.rspec, .rspec or `.rspec-local`. +# +# The following line is provided for convenience purposes. It has the downside +# of increasing the boot-up time by auto-requiring all files in the support +# directory. Alternatively, in the individual `*_spec.rb` files, manually +# require only the support files necessary. +# +ENGINE_RAILS_ROOT=File.join(File.dirname(__FILE__), '../') +Dir[File.join(ENGINE_RAILS_ROOT, "spec/support/**/*.rb")].each {|f| require f } + +RSpec.configure do |config| + require 'database_cleaner' + + config.before(:suite) do + DatabaseCleaner.strategy = :transaction + DatabaseCleaner.clean_with(:truncation) + end + # RSpec Rails can automatically mix in different behaviours to your tests + # based on their file location, for example enabling you to call `get` and + # `post` in specs under `spec/controllers`. + # + # You can disable this behaviour by removing the line below, and instead + # explicitly tag your specs with their type, e.g.: + # + # RSpec.describe UsersController, :type => :controller do + # # ... + # end + # + # The different available types are documented in the features, such as in + # https://relishapp.com/rspec/rspec-rails/docs + config.infer_spec_type_from_file_location! +end diff --git a/spec/routing/simple_editable/engine_routes_spec.rb b/spec/routing/simple_editable/engine_routes_spec.rb new file mode 100644 index 0000000..3b96786 --- /dev/null +++ b/spec/routing/simple_editable/engine_routes_spec.rb @@ -0,0 +1,17 @@ +require 'rails_helper' + +RSpec.describe SimpleEditable, 'routing to profiles', type: :routing do + routes { SimpleEditable::Engine.routes } + + it 'route to update inline text' do + routes { SimpleEditable::Engine.routes } + Rails.application.reload_routes! + expect(update_inline_text_path).to eq '/simple_editable/update_inline_text' + end + + it 'route to update inline object' do + routes { SimpleEditable::Engine.routes } + Rails.application.reload_routes! + expect(update_inline_object_path).to eq '/simple_editable/update_inline_object' + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..116f4d0 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,85 @@ +# This file was generated by the `rails generate rspec:install` command. Conventionally, all +# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. +# The generated `.rspec` file contains `--require spec_helper` which will cause this +# file to always be loaded, without a need to explicitly require it in any files. +# +# Given that it is always loaded, you are encouraged to keep this file as +# light-weight as possible. Requiring heavyweight dependencies from this file +# will add to the boot time of your test suite on EVERY test run, even for an +# individual file that may not need all of that loaded. Instead, consider making +# a separate helper file that requires the additional dependencies and performs +# the additional setup, and require it from the spec files that actually need it. +# +# The `.rspec` file also contains a few flags that are not defaults but that +# users commonly want. +# +# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + # rspec-expectations config goes here. You can use an alternate + # assertion/expectation library such as wrong or the stdlib/minitest + # assertions if you prefer. + config.expect_with :rspec do |expectations| + # This option will default to `true` in RSpec 4. It makes the `description` + # and `failure_message` of custom matchers include text for helper methods + # defined using `chain`, e.g.: + # be_bigger_than(2).and_smaller_than(4).description + # # => "be bigger than 2 and smaller than 4" + # ...rather than: + # # => "be bigger than 2" + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with :rspec do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = false + end + +# The settings below are suggested to provide a good initial experience +# with RSpec, but feel free to customize to your heart's content. +=begin + # These two settings work together to allow you to limit a spec run + # to individual examples or groups you care about by tagging them with + # `:focus` metadata. When nothing is tagged with `:focus`, all examples + # get run. + config.filter_run :focus + config.run_all_when_everything_filtered = true + + # Limits the available syntax to the non-monkey patched syntax that is recommended. + # For more details, see: + # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax + # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching + config.disable_monkey_patching! + + # Many RSpec users commonly either run the entire suite or an individual + # file, and it's useful to allow more verbose output when running an + # individual spec file. + if config.files_to_run.one? + # Use the documentation formatter for detailed output, + # unless a formatter has already been configured + # (e.g. via a command-line flag). + config.default_formatter = 'doc' + end + + # Print the 10 slowest examples and example groups at the + # end of the spec run, to help surface which specs are running + # particularly slow. + config.profile_examples = 10 + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = :random + + # Seed global randomization in this process using the `--seed` CLI option. + # Setting this allows you to use `--seed` to deterministically reproduce + # test failures related to randomization by passing the same `--seed` value + # as the one that triggered the failure. + Kernel.srand config.seed +=end +end