├── .gitignore ├── .travis.yml ├── Gemfile ├── Gemfile.lock ├── LICENSE ├── MIT-LICENSE ├── README.md ├── Rakefile ├── activeform-rails.gemspec ├── lib ├── activeform-rails.rb ├── activeform-rails │ ├── form.rb │ ├── unpersistent_model.rb │ ├── validate_uniqueness.rb │ └── version.rb └── tasks │ └── activeform_tasks.rake └── spec ├── activeform-rails ├── form_spec.rb ├── validate_uniqueness_spec.rb └── validation_spec.rb ├── dummy ├── Gemfile ├── Gemfile.lock ├── README.rdoc ├── Rakefile ├── app │ ├── assets │ │ ├── images │ │ │ └── .keep │ │ ├── javascripts │ │ │ └── application.js │ │ └── stylesheets │ │ │ ├── application.css │ │ │ └── scaffold.css │ ├── controllers │ │ ├── application_controller.rb │ │ ├── categories_controller.rb │ │ ├── concerns │ │ │ └── .keep │ │ └── users_controller.rb │ ├── forms │ │ ├── category_form.rb │ │ └── user_form.rb │ ├── helpers │ │ └── application_helper.rb │ ├── mailers │ │ └── .keep │ ├── models │ │ ├── .keep │ │ ├── category.rb │ │ ├── concerns │ │ │ └── .keep │ │ └── user.rb │ └── views │ │ ├── categories │ │ ├── _form.html.erb │ │ ├── edit.html.erb │ │ ├── index.html.erb │ │ ├── new.html.erb │ │ └── show.html.erb │ │ ├── layouts │ │ └── application.html.erb │ │ └── users │ │ ├── _form.html.erb │ │ ├── edit.html.erb │ │ ├── index.html.erb │ │ ├── new.html.erb │ │ └── show.html.erb ├── bin │ ├── bundle │ ├── rails │ └── rake ├── config.ru ├── config │ ├── application.rb │ ├── boot.rb │ ├── database.yml │ ├── environment.rb │ ├── environments │ │ ├── development.rb │ │ ├── production.rb │ │ └── test.rb │ ├── initializers │ │ ├── backtrace_silencers.rb │ │ ├── filter_parameter_logging.rb │ │ ├── inflections.rb │ │ ├── mime_types.rb │ │ ├── secret_token.rb │ │ ├── session_store.rb │ │ └── wrap_parameters.rb │ ├── locales │ │ └── en.yml │ └── routes.rb ├── db │ ├── development.sqlite3 │ ├── migrate │ │ ├── 20140216201540_create_users.rb │ │ └── 20140222122256_create_categories.rb │ └── schema.rb ├── lib │ └── assets │ │ └── .keep ├── log │ └── .keep ├── public │ ├── 404.html │ ├── 422.html │ ├── 500.html │ └── favicon.ico └── test │ ├── controllers │ ├── categories_controller_test.rb │ └── users_controller_test.rb │ ├── fixtures │ ├── categories.yml │ └── users.yml │ ├── helpers │ ├── categories_helper_test.rb │ └── users_helper_test.rb │ └── models │ ├── category_test.rb │ └── user_test.rb └── spec_helper.rb /.gitignore: -------------------------------------------------------------------------------- 1 | .bundle/ 2 | log/*.log 3 | pkg/ 4 | test/dummy/db/*.sqlite3 5 | test/dummy/db/*.sqlite3-journal 6 | test/dummy/log/*.log 7 | test/dummy/tmp/ 8 | test/dummy/.sass-cache 9 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | rvm: 3 | - 2.0.0 4 | - 2.1.1 5 | script: "bundle exec rspec" 6 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | # Declare your gem's dependencies in activeform.gemspec. 4 | # Bundler will treat runtime dependencies like base dependencies, and 5 | # development dependencies will be added by default to the :development group. 6 | gemspec 7 | 8 | # Declare any dependencies that are still in development here instead of in 9 | # your gemspec. These might include edge Rails or gems from your path or 10 | # Git. Remember to move these dependencies to your gemspec before releasing 11 | # your gem to rubygems.org. 12 | 13 | # To use debugger 14 | # gem 'debugger' 15 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | PATH 2 | remote: . 3 | specs: 4 | activeform-rails (0.0.4) 5 | activemodel (>= 3) 6 | activesupport (>= 3) 7 | 8 | GEM 9 | remote: https://rubygems.org/ 10 | specs: 11 | activemodel (4.1.7) 12 | activesupport (= 4.1.7) 13 | builder (~> 3.1) 14 | activerecord (4.1.7) 15 | activemodel (= 4.1.7) 16 | activesupport (= 4.1.7) 17 | arel (~> 5.0.0) 18 | activesupport (4.1.7) 19 | i18n (~> 0.6, >= 0.6.9) 20 | json (~> 1.7, >= 1.7.7) 21 | minitest (~> 5.1) 22 | thread_safe (~> 0.1) 23 | tzinfo (~> 1.1) 24 | arel (5.0.1.20140414130214) 25 | builder (3.2.2) 26 | database_cleaner (1.3.0) 27 | diff-lcs (1.2.5) 28 | i18n (0.6.11) 29 | json (1.8.1) 30 | minitest (5.4.2) 31 | rspec (2.99.0) 32 | rspec-core (~> 2.99.0) 33 | rspec-expectations (~> 2.99.0) 34 | rspec-mocks (~> 2.99.0) 35 | rspec-core (2.99.2) 36 | rspec-expectations (2.99.2) 37 | diff-lcs (>= 1.1.3, < 2.0) 38 | rspec-mocks (2.99.2) 39 | sqlite3 (1.3.10) 40 | thread_safe (0.3.4) 41 | tzinfo (1.2.2) 42 | thread_safe (~> 0.1) 43 | 44 | PLATFORMS 45 | ruby 46 | 47 | DEPENDENCIES 48 | activeform-rails! 49 | activerecord (>= 3) 50 | database_cleaner (~> 1.2) 51 | rspec (~> 2) 52 | sqlite3 (~> 1) 53 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright (c) 2014 Guirec Corbel 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MIT-LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2014 YOURNAME 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ActiveForm 2 | 3 | Apply form objects to ActiveModel. Form objects have responsability to decouple the form logic from the model. It will help you to simplify your models. 4 | 5 | ## Installation 6 | 7 | Add this line to you Gemfile : 8 | 9 | ```ruby 10 | gem 'activeform-rails' 11 | ``` 12 | 13 | Please make sure you are running a compatible version of Ruby, see below. 14 | 15 | ## Quick example 16 | 17 | In order to manage category and users, you can create an object like this : 18 | 19 | ```ruby 20 | class Form 21 | include ActiveForm::Form 22 | 23 | properties :name, on: :user 24 | properties :title, on: :category 25 | 26 | self.main_model = :user 27 | end 28 | ``` 29 | 30 | Now, you can do this : 31 | 32 | ```ruby 33 | user = User.new 34 | category = Category.new 35 | form = Form.new(user: user, category: category) 36 | form.user # return the user 37 | form.user == user # return true 38 | 39 | form.user.name # return nil 40 | form.name # return nil 41 | form.fill_attributes(name: 'GCorbel') 42 | form.user.name # return 'GCorbel' 43 | form.name # return 'GCorbel' 44 | 45 | form.valid? # return true 46 | form.save # save all models and return true 47 | ``` 48 | 49 | ## Example without backing by an ActiveModel 50 | 51 | If you would like to use form objects to provide validations to simple objects, simply omit the `on` argument and `main_model` definition as follows : 52 | 53 | ```ruby 54 | class Form 55 | include ActiveForm::Form 56 | properties :name, :title 57 | validates_presence_of :title 58 | end 59 | 60 | form = Form.new(name: 'John') 61 | form.name # return John 62 | form.title # return nil 63 | form.valid? # return false 64 | ``` 65 | 66 | ## Use validations 67 | 68 | Validations works like a normal ActiveModel class. So, you can do this : 69 | 70 | ```ruby 71 | class Form 72 | include ActiveForm::Form 73 | 74 | properties :name, on: :user 75 | 76 | validates :name, presence: true 77 | 78 | self.main_model = :user 79 | end 80 | ``` 81 | 82 | And use it like this : 83 | 84 | 85 | ```ruby 86 | user = User.new 87 | form = Form.new(user: user) 88 | form.valid? # return false 89 | form.errors # return # 90 | form.fill_attributes(name: 'GCorbel') 91 | form.valid? # return true 92 | ``` 93 | 94 | 95 | To validate the unicity or a property, you can do this : 96 | 97 | ```ruby 98 | class Form 99 | include ActiveForm::Form 100 | include ActiveForm::ValidateUniqueness 101 | properties :name, on: :user 102 | validates_uniqueness_of :name, :user 103 | end 104 | ``` 105 | 106 | The `validates_uniqueness_of` take two parameters, the first is the property which should be unique and the second is the model for this property. 107 | 108 | ## Saving forms 109 | 110 | There is two methods to save forms, `save` and `save!`. `save` will return true or false if the model is valid or not. `save!` will return an error and will rollback all change mades. 111 | 112 | You can customize those methods by adding a block like this : 113 | 114 | ```ruby 115 | class Form 116 | include ActiveForm::Form 117 | 118 | properties :name, on: :user 119 | 120 | self.main_model = :user 121 | end 122 | 123 | form = Form.new(user: User.new) 124 | form.save do |f| 125 | f.user # return the user 126 | end 127 | ``` 128 | 129 | You can also override the save method like this : 130 | 131 | ```ruby 132 | class Form 133 | include ActiveForm::Form 134 | 135 | properties :name, on: :user 136 | 137 | self.main_model = :user 138 | 139 | def save 140 | super do 141 | user.save 142 | end 143 | end 144 | end 145 | ``` 146 | 147 | **Take care :** If your logic is too complex, it's probably better to use a service object. 148 | 149 | ## has_many relationship 150 | 151 | To manage a has_many relationship, you can do it like this : 152 | 153 | ```ruby 154 | class Form 155 | include ActiveForm::Form 156 | properties :name, on: :category 157 | self.main_model = :category 158 | 159 | attr_accessor :user_ids 160 | 161 | def save 162 | super do 163 | category.users = user_ids.map { |user_id| User.find(user_id) } 164 | category.save 165 | end 166 | end 167 | end 168 | ``` 169 | 170 | ## Alias properties 171 | 172 | Sometimes it's useful to create an alias for a method, you can do it like this : 173 | 174 | ```ruby 175 | class Form 176 | include ActiveForm::Form 177 | properties :name, on: :category 178 | alias_property :category_name, :name 179 | self.main_model = :category 180 | end 181 | 182 | form = Form.new(category: Category.new(name: 'bacon')) 183 | form.category_name # return 'bacon' 184 | form.category_name = 'beef' 185 | form.category_name # return 'beef' 186 | ``` 187 | 188 | ## Complete Example 189 | 190 | You can find an example of a working application in the spec/dummy directory. 191 | 192 | ## Requirements 193 | 194 | Ruby 2 or greater. 195 | 196 | ## Contributing 197 | 198 | 1. Fork it 199 | 2. Create your feature branch (`git checkout -b my-new-feature`) 200 | 3. Commit your changes (`git commit -am 'Add some feature'`) 201 | 4. Push to the branch (`git push origin my-new-feature`) 202 | 5. Create new Pull Request 203 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | begin 2 | require 'bundler/setup' 3 | rescue LoadError 4 | puts 'You must `gem install bundler` and `bundle install` to run rake tasks' 5 | end 6 | 7 | require 'rdoc/task' 8 | 9 | RDoc::Task.new(:rdoc) do |rdoc| 10 | rdoc.rdoc_dir = 'rdoc' 11 | rdoc.title = 'Activeform' 12 | rdoc.options << '--line-numbers' 13 | rdoc.rdoc_files.include('README.rdoc') 14 | rdoc.rdoc_files.include('lib/**/*.rb') 15 | end 16 | 17 | 18 | 19 | 20 | Bundler::GemHelper.install_tasks 21 | 22 | require 'rake/testtask' 23 | 24 | Rake::TestTask.new(:test) do |t| 25 | t.libs << 'lib' 26 | t.libs << 'test' 27 | t.pattern = 'test/**/*_test.rb' 28 | t.verbose = false 29 | end 30 | 31 | 32 | task default: :test 33 | -------------------------------------------------------------------------------- /activeform-rails.gemspec: -------------------------------------------------------------------------------- 1 | $:.push File.expand_path("../lib", __FILE__) 2 | 3 | # Maintain your gem's version: 4 | require "activeform-rails/version" 5 | 6 | # Describe your gem and declare its dependencies: 7 | Gem::Specification.new do |s| 8 | s.name = "activeform-rails" 9 | s.version = Activeform::VERSION 10 | s.authors = ["Guirec Corbel"] 11 | s.email = ["guirec.corbel@gmail.com"] 12 | s.homepage = "https://github.com/GCorbel/ActiveForm" 13 | s.summary = "Form Objects for ActiveModel" 14 | s.description = "Enable to use the concept of form objects with ActiveModel" 15 | s.license = "MIT" 16 | 17 | s.files = Dir["lib/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"] 18 | s.test_files = Dir["spec/**/*"] 19 | 20 | s.add_dependency "activemodel", ">= 3" 21 | s.add_dependency "activesupport", ">= 3" 22 | 23 | s.add_development_dependency "sqlite3", "~> 1" 24 | s.add_development_dependency "rspec", "~> 2" 25 | s.add_development_dependency "activerecord", ">= 3" 26 | s.add_development_dependency "database_cleaner", "~> 1.2" 27 | end 28 | -------------------------------------------------------------------------------- /lib/activeform-rails.rb: -------------------------------------------------------------------------------- 1 | module ActiveForm 2 | class CannotBePersisted < Exception; end 3 | end 4 | 5 | require 'active_support/core_ext/module/delegation' 6 | require 'active_record' 7 | require 'activeform-rails/form' 8 | require 'activeform-rails/unpersistent_model' 9 | require 'activeform-rails/validate_uniqueness' 10 | -------------------------------------------------------------------------------- /lib/activeform-rails/form.rb: -------------------------------------------------------------------------------- 1 | module ActiveForm::Form 2 | def self.included(base) 3 | base.class_eval do 4 | extend ActiveModel::Naming 5 | include ActiveModel::Conversion 6 | include ActiveModel::Validations 7 | end 8 | base.extend ClassMethods 9 | end 10 | 11 | module ClassMethods 12 | delegate :model_name, :reflect_on_association, to: :main_class 13 | attr_accessor :main_class, :reflected_class, :main_model 14 | 15 | def properties(*attributes, prefix: false, on: nil) 16 | if on.nil? 17 | attr_accessor *attributes 18 | else 19 | delegate_to_model(attributes, on, prefix) 20 | end 21 | end 22 | 23 | def i18n_scope 24 | :activerecord 25 | end 26 | 27 | def models 28 | @models ||= [] 29 | end 30 | 31 | def main_model 32 | @main_model ||= UnpersistentModel.new(self) 33 | end 34 | 35 | def main_class 36 | @main_class ||= if main_model.kind_of?(Symbol) 37 | main_model.to_s.camelize.constantize 38 | else 39 | @main_model 40 | end 41 | end 42 | 43 | def alias_property(new_method, old_method) 44 | alias_method new_method.to_sym, old_method.to_sym 45 | alias_method "#{new_method}=".to_sym, "#{old_method}=".to_sym 46 | end 47 | 48 | private 49 | 50 | def add_model_on_list(model_name) 51 | models << model_name unless models.include?(model_name) 52 | end 53 | 54 | def add_accessor(model_name) 55 | attr_accessor model_name 56 | end 57 | 58 | def assign_delegators(attributes, model_name, prefix) 59 | attributes.each do |attribute| 60 | delegate attribute, to: model_name, prefix: prefix 61 | delegate "#{attribute}=", to: model_name, prefix: prefix 62 | end 63 | end 64 | 65 | def delegate_to_model(attributes, on, prefix) 66 | assign_delegators(attributes, on, prefix) 67 | add_model_on_list(on) 68 | add_accessor(on) 69 | end 70 | end 71 | 72 | delegate :to_key, :to_param, :id, :persisted?, to: :main_model 73 | 74 | def initialize(attributes = {}) 75 | assign_from_hash(attributes) 76 | end 77 | 78 | def fill_attributes(params) 79 | assign_from_hash(params) 80 | end 81 | 82 | def save(&block) 83 | ensure_persistable 84 | valid?.tap do 85 | call_action_or_block(:save, &block) 86 | end 87 | end 88 | 89 | def save!(&block) 90 | ensure_persistable 91 | ActiveRecord::Base.transaction do 92 | call_action_or_block(:save!, &block) 93 | end 94 | end 95 | 96 | def main_model 97 | if self.class.main_model.kind_of?(Symbol) 98 | send(self.class.main_model) 99 | else 100 | self.class.main_model 101 | end 102 | end 103 | 104 | def new_record? 105 | !persisted? 106 | end 107 | 108 | private 109 | 110 | def ensure_persistable 111 | message = 'The Form object is not backed by models so cannot be saved' 112 | if self.class.models.empty? 113 | raise ActiveForm::CannotBePersisted.new(message) 114 | end 115 | end 116 | 117 | def each_models 118 | self.class.models.each do |model_name| 119 | yield(send(model_name)) 120 | end 121 | end 122 | 123 | def assign_from_hash(hash) 124 | hash.each { |key, value| send("#{key}=", value) } 125 | end 126 | 127 | def call_action_or_block(action, &block) 128 | block_given? ? block.call(self) : each_models(&action) 129 | end 130 | end 131 | -------------------------------------------------------------------------------- /lib/activeform-rails/unpersistent_model.rb: -------------------------------------------------------------------------------- 1 | # UnpersistentModel allows the ActiveForm::Form object to delegate methods to 2 | # this object with expected results 3 | # 4 | # - #model_name is used by Form helpers & Rails frequently to define the form namespace 5 | # - #to_key, #to_param, #id should aways return nil as the Form cannot be persisted unless backed by ActiveModel 6 | # - #persisted? should aways return false as the Form cannot be persisted unless backed by ActiveModel 7 | # 8 | module ActiveForm::Form 9 | class UnpersistentModel 10 | attr_reader :to_key, :to_param, :id 11 | 12 | def initialize(base_klass) 13 | @base_class = base_klass 14 | end 15 | 16 | def model_name 17 | ActiveModel::Name.new(@base_class) 18 | end 19 | 20 | def persisted? 21 | false 22 | end 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /lib/activeform-rails/validate_uniqueness.rb: -------------------------------------------------------------------------------- 1 | module ActiveForm 2 | module ValidateUniqueness 3 | def self.included(base) 4 | base.class_eval do 5 | extend ClassMethods 6 | end 7 | end 8 | 9 | module ClassMethods 10 | def validates_uniqueness_of(attribute, model_name, options = {}) 11 | validates_each attribute, options do |form, attr, value| 12 | @form = form 13 | @model = form.send(model_name) 14 | @klass = @model.class 15 | @hash = { attribute => value } 16 | add_error_message(attribute) if another_model? 17 | end 18 | end 19 | 20 | private 21 | 22 | def another_model? 23 | @model.persisted? ? another_model_without_itself : any_model? 24 | end 25 | 26 | def another_model_without_itself 27 | @klass.where(@hash).to_a.delete_if { |m| m.id == @model.id }.count >= 1 28 | end 29 | 30 | def any_model? 31 | @klass.exists?(@hash) 32 | end 33 | 34 | def error_message 35 | I18n.t('activerecord.errors.messages.exclusion') 36 | end 37 | 38 | def add_error_message(attribute) 39 | @form.errors.add(attribute, error_message) 40 | end 41 | end 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /lib/activeform-rails/version.rb: -------------------------------------------------------------------------------- 1 | module Activeform 2 | VERSION = "0.0.5" 3 | end 4 | -------------------------------------------------------------------------------- /lib/tasks/activeform_tasks.rake: -------------------------------------------------------------------------------- 1 | # desc "Explaining what the task does" 2 | # task :activeform do 3 | # # Task goes here 4 | # end 5 | -------------------------------------------------------------------------------- /spec/activeform-rails/form_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe ActiveForm do 4 | context 'backed by models' do 5 | class Form 6 | include ActiveForm::Form 7 | properties :name, on: :user 8 | properties :title, on: :category 9 | 10 | attr_accessor :user, :category 11 | 12 | self.main_model = :user 13 | end 14 | 15 | describe ".main_class" do 16 | it "reflect assoctions of the main model" do 17 | user = User.new 18 | category = Category.new 19 | user.categories = [category] 20 | expect(Form.reflect_on_association(:has_many)).to eq \ 21 | User.reflect_on_association(:has_many) 22 | end 23 | 24 | context "when a main class is specified" do 25 | it "take the specified class" do 26 | Form.main_class = Category 27 | expect(Form.main_class).to eq Category 28 | end 29 | end 30 | 31 | context "when there is only a main model specified" do 32 | it "build the main class from the main model" do 33 | Form.main_model = :category 34 | expect(Form.main_class).to eq Category 35 | end 36 | end 37 | end 38 | 39 | describe "#fill_attributes" do 40 | it "assign variable to models" do 41 | user = User.new 42 | category = Category.new 43 | form = Form.new(user: user, category: category) 44 | form.fill_attributes(name: "Martin") 45 | expect(user.name).to eq "Martin" 46 | end 47 | end 48 | 49 | describe "#save" do 50 | context "when the form is valid" do 51 | context "when no block is given" do 52 | it "save all models" do 53 | user = User.new 54 | category = Category.new 55 | form = Form.new(user: user, category: category) 56 | expect(user).to receive(:save) 57 | expect(category).to receive(:save) 58 | form.save 59 | end 60 | 61 | it "return true" do 62 | form = Form.new(user: User.new, category: Category.new) 63 | allow(form).to receive(:valid?).and_return(true) 64 | expect(form.save).to eq true 65 | end 66 | end 67 | 68 | context "when a block is given" do 69 | it "use the block" do 70 | user = User.new 71 | form = Form.new(user: user, category: Category.new) 72 | allow(form).to receive(:valid?).and_return(true) 73 | expect(user).to receive(:process) 74 | form.save { |f| f.user.process } 75 | end 76 | end 77 | end 78 | 79 | context "when the form is invalid" do 80 | it "return false" do 81 | form = Form.new(user: User.new, category: Category.new) 82 | allow(form).to receive(:valid?).and_return(false) 83 | expect(form.save).to eq false 84 | end 85 | end 86 | end 87 | 88 | describe "#save!" do 89 | context "when no block is given" do 90 | it "save all models" do 91 | user = User.new 92 | category = Category.new 93 | form = Form.new(user: user, category: category) 94 | expect(user).to receive(:save!) 95 | expect(category).to receive(:save!) 96 | form.save! 97 | end 98 | 99 | it "is surrounded by a transaction" do 100 | expect(ActiveRecord::Base).to receive(:transaction).at_least(:once). 101 | and_yield 102 | form = Form.new(user: User.new, category: Category.new) 103 | form.save! 104 | end 105 | end 106 | 107 | context "when a block is given" do 108 | it "use the block" do 109 | user = User.new 110 | form = Form.new(user: user, category: Category.new) 111 | expect(user).to receive(:process) 112 | form.save! { |f| f.user.process } 113 | end 114 | end 115 | end 116 | 117 | describe "#main_model" do 118 | it "give the main model" do 119 | Form.main_model = :category 120 | category = Category.new 121 | form = Form.new(user: User.new, category: category) 122 | expect(form.main_model).to eq category 123 | end 124 | end 125 | 126 | describe "#persisted?" do 127 | it "should be false when not saved" do 128 | form = Form.new(user: User.new, category: Category.new) 129 | expect(form.persisted?).to eq false 130 | end 131 | 132 | it "should be true when not saved" do 133 | form = Form.new(user: User.new, category: Category.new) 134 | form.save! 135 | expect(form.persisted?).to eq true 136 | end 137 | end 138 | 139 | describe "#new_record?" do 140 | it "should be true when not saved" do 141 | form = Form.new(user: User.new, category: Category.new) 142 | expect(form.new_record?).to eq true 143 | end 144 | 145 | it "should be false when not saved" do 146 | form = Form.new(user: User.new, category: Category.new) 147 | form.save! 148 | expect(form.new_record?).to eq false 149 | end 150 | end 151 | 152 | describe "class method #alias_method" do 153 | class AliasForm 154 | include ActiveForm::Form 155 | properties :name, on: :user 156 | alias_property :full_name, :name 157 | self.main_model = :user 158 | end 159 | 160 | subject do 161 | user = User.new(name: 'John') 162 | AliasForm.new(user: user) 163 | end 164 | 165 | it "should allow alias name to be used for read operations" do 166 | expect(subject.full_name).to eq 'John' 167 | end 168 | 169 | it "should allow alias name to be used for write operations" do 170 | subject.full_name = 'Mike' 171 | expect(subject.full_name).to eq 'Mike' 172 | end 173 | end 174 | end 175 | 176 | context 'without any underlying models' do 177 | class FormNoModels 178 | include ActiveForm::Form 179 | properties :name, :title 180 | end 181 | 182 | it 'it should allow initialization without any arg' do 183 | form = FormNoModels.new 184 | expect(form.name).to eq nil 185 | expect(form.title).to eq nil 186 | end 187 | 188 | it 'it should args to be set on initialize' do 189 | form = FormNoModels.new(name: 'John', title: 'CEO') 190 | expect(form.name).to eq 'John' 191 | expect(form.title).to eq 'CEO' 192 | end 193 | 194 | it 'should raise an exception on save' do 195 | expect do 196 | FormNoModels.new.save 197 | end.to raise_exception(ActiveForm::CannotBePersisted) 198 | end 199 | 200 | it 'should raise an exception on save!' do 201 | expect do 202 | FormNoModels.new.save! 203 | end.to raise_exception(ActiveForm::CannotBePersisted) 204 | end 205 | 206 | it 'should delegate class method #model_name' do 207 | expect(FormNoModels.model_name.to_s).to eq 'FormNoModels' 208 | end 209 | 210 | it 'should delegate #to_key' do 211 | expect(FormNoModels.new.to_key).to eq nil 212 | end 213 | 214 | it 'should delegate #to_param' do 215 | expect(FormNoModels.new.to_param).to eq nil 216 | end 217 | 218 | it 'should delegate #id' do 219 | expect(FormNoModels.new.id).to eq nil 220 | end 221 | 222 | it 'should always return false to #persisted?' do 223 | expect(FormNoModels.new.persisted?).to be_falsey 224 | end 225 | end 226 | end 227 | -------------------------------------------------------------------------------- /spec/activeform-rails/validate_uniqueness_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe ActiveForm::ValidateUniqueness do 4 | class ValidateUniquenessForm 5 | include ActiveForm::Form 6 | include ActiveForm::ValidateUniqueness 7 | properties :name, :firstname, on: :user 8 | validates_uniqueness_of :name, :user, allow_nil: true 9 | validates_uniqueness_of :firstname, :user, allow_nil: true 10 | end 11 | 12 | context "when there is no user" do 13 | it "is valid" do 14 | user = User.new 15 | form = ValidateUniquenessForm.new(user: user) 16 | expect(form).to be_valid 17 | end 18 | end 19 | 20 | context "when there is user with the same email" do 21 | context "when the user has an available name" do 22 | it "is valid" do 23 | user = User.create(name: 'name') 24 | form = ValidateUniquenessForm.new(user: user) 25 | expect(form).to be_valid 26 | end 27 | end 28 | 29 | context "when the user has unavailable name" do 30 | it "is not valid" do 31 | User.create(name: 'name') 32 | user = User.new(name: 'name') 33 | form = ValidateUniquenessForm.new(user: user) 34 | expect(form).to_not be_valid 35 | end 36 | 37 | it "assign an error to the name" do 38 | User.create(name: 'name') 39 | user = User.new(name: 'name') 40 | form = ValidateUniquenessForm.new(user: user) 41 | form.valid? 42 | expect(form.errors[:name]).to_not be_empty 43 | end 44 | end 45 | end 46 | end 47 | -------------------------------------------------------------------------------- /spec/activeform-rails/validation_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe 'ActiveForm ActiveRecord validators' do 4 | context 'backed by models' do 5 | class ValidationForm 6 | include ActiveForm::Form 7 | properties :name, on: :user 8 | validates_presence_of :name 9 | end 10 | 11 | context "when name is empty" do 12 | it "is not valid" do 13 | user = User.new 14 | form = ValidationForm.new(user: user) 15 | expect(form).to_not be_valid 16 | end 17 | end 18 | 19 | context "when name is filled in" do 20 | it "is valid" do 21 | user = User.new 22 | user.name = 'John' 23 | form = ValidationForm.new(user: user) 24 | expect(form).to be_valid 25 | end 26 | end 27 | end 28 | 29 | context 'without any underlying models' do 30 | class ValidationFormNoModels 31 | include ActiveForm::Form 32 | properties :name 33 | validates_presence_of :name 34 | end 35 | 36 | context "when name is empty" do 37 | it "is not valid" do 38 | form = ValidationFormNoModels.new 39 | expect(form).to_not be_valid 40 | end 41 | end 42 | 43 | context "when name is filled in" do 44 | it "is valid" do 45 | form = ValidationFormNoModels.new(name: 'John') 46 | expect(form).to be_valid 47 | end 48 | end 49 | end 50 | end 51 | -------------------------------------------------------------------------------- /spec/dummy/Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gem 'rails', '>= 4.0.0' 4 | gem 'sqlite3' 5 | gem 'simple_form' 6 | 7 | gem 'pry', group: 'development' 8 | -------------------------------------------------------------------------------- /spec/dummy/Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | actionmailer (4.1.7) 5 | actionpack (= 4.1.7) 6 | actionview (= 4.1.7) 7 | mail (~> 2.5, >= 2.5.4) 8 | actionpack (4.1.7) 9 | actionview (= 4.1.7) 10 | activesupport (= 4.1.7) 11 | rack (~> 1.5.2) 12 | rack-test (~> 0.6.2) 13 | actionview (4.1.7) 14 | activesupport (= 4.1.7) 15 | builder (~> 3.1) 16 | erubis (~> 2.7.0) 17 | activemodel (4.1.7) 18 | activesupport (= 4.1.7) 19 | builder (~> 3.1) 20 | activerecord (4.1.7) 21 | activemodel (= 4.1.7) 22 | activesupport (= 4.1.7) 23 | arel (~> 5.0.0) 24 | activesupport (4.1.7) 25 | i18n (~> 0.6, >= 0.6.9) 26 | json (~> 1.7, >= 1.7.7) 27 | minitest (~> 5.1) 28 | thread_safe (~> 0.1) 29 | tzinfo (~> 1.1) 30 | arel (5.0.1.20140414130214) 31 | builder (3.2.2) 32 | coderay (1.1.0) 33 | erubis (2.7.0) 34 | hike (1.2.3) 35 | i18n (0.6.11) 36 | json (1.8.1) 37 | mail (2.6.3) 38 | mime-types (>= 1.16, < 3) 39 | method_source (0.8.2) 40 | mime-types (2.4.3) 41 | minitest (5.4.2) 42 | multi_json (1.10.1) 43 | pry (0.10.1) 44 | coderay (~> 1.1.0) 45 | method_source (~> 0.8.1) 46 | slop (~> 3.4) 47 | rack (1.5.2) 48 | rack-test (0.6.2) 49 | rack (>= 1.0) 50 | rails (4.1.7) 51 | actionmailer (= 4.1.7) 52 | actionpack (= 4.1.7) 53 | actionview (= 4.1.7) 54 | activemodel (= 4.1.7) 55 | activerecord (= 4.1.7) 56 | activesupport (= 4.1.7) 57 | bundler (>= 1.3.0, < 2.0) 58 | railties (= 4.1.7) 59 | sprockets-rails (~> 2.0) 60 | railties (4.1.7) 61 | actionpack (= 4.1.7) 62 | activesupport (= 4.1.7) 63 | rake (>= 0.8.7) 64 | thor (>= 0.18.1, < 2.0) 65 | rake (10.3.2) 66 | simple_form (3.0.2) 67 | actionpack (~> 4.0) 68 | activemodel (~> 4.0) 69 | slop (3.6.0) 70 | sprockets (2.12.3) 71 | hike (~> 1.2) 72 | multi_json (~> 1.0) 73 | rack (~> 1.0) 74 | tilt (~> 1.1, != 1.3.0) 75 | sprockets-rails (2.2.0) 76 | actionpack (>= 3.0) 77 | activesupport (>= 3.0) 78 | sprockets (>= 2.8, < 4.0) 79 | sqlite3 (1.3.10) 80 | thor (0.19.1) 81 | thread_safe (0.3.4) 82 | tilt (1.4.1) 83 | tzinfo (1.2.2) 84 | thread_safe (~> 0.1) 85 | 86 | PLATFORMS 87 | ruby 88 | 89 | DEPENDENCIES 90 | pry 91 | rails (>= 4.0.0) 92 | simple_form 93 | sqlite3 94 | -------------------------------------------------------------------------------- /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/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 | Dummy::Application.load_tasks 7 | -------------------------------------------------------------------------------- /spec/dummy/app/assets/images/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GCorbel/activeform-rails/d907b7dd4c9fdffe79b0c08d5a54baa86b415b75/spec/dummy/app/assets/images/.keep -------------------------------------------------------------------------------- /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 vendor/assets/javascripts of plugins, if any, 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/sstephenson/sprockets#sprockets-directives) for details 11 | // about supported directives. 12 | // 13 | //= require_tree . 14 | -------------------------------------------------------------------------------- /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 vendor/assets/stylesheets of plugins, if any, 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 top of the 9 | * compiled file, but it's generally better to create a new file per style scope. 10 | * 11 | *= require_self 12 | *= require_tree . 13 | */ 14 | -------------------------------------------------------------------------------- /spec/dummy/app/assets/stylesheets/scaffold.css: -------------------------------------------------------------------------------- 1 | body { background-color: #fff; color: #333; } 2 | 3 | body, p, ol, ul, td { 4 | font-family: verdana, arial, helvetica, sans-serif; 5 | font-size: 13px; 6 | line-height: 18px; 7 | } 8 | 9 | pre { 10 | background-color: #eee; 11 | padding: 10px; 12 | font-size: 11px; 13 | } 14 | 15 | a { color: #000; } 16 | a:visited { color: #666; } 17 | a:hover { color: #fff; background-color:#000; } 18 | 19 | div.field, div.actions { 20 | margin-bottom: 10px; 21 | } 22 | 23 | #notice { 24 | color: green; 25 | } 26 | 27 | .field_with_errors { 28 | padding: 2px; 29 | background-color: red; 30 | display: table; 31 | } 32 | 33 | #error_explanation { 34 | width: 450px; 35 | border: 2px solid red; 36 | padding: 7px; 37 | padding-bottom: 0; 38 | margin-bottom: 20px; 39 | background-color: #f0f0f0; 40 | } 41 | 42 | #error_explanation h2 { 43 | text-align: left; 44 | font-weight: bold; 45 | padding: 5px 5px 5px 15px; 46 | font-size: 12px; 47 | margin: -7px; 48 | margin-bottom: 0px; 49 | background-color: #c00; 50 | color: #fff; 51 | } 52 | 53 | #error_explanation ul li { 54 | font-size: 12px; 55 | list-style: square; 56 | } 57 | -------------------------------------------------------------------------------- /spec/dummy/app/controllers/application_controller.rb: -------------------------------------------------------------------------------- 1 | class ApplicationController < ActionController::Base 2 | # Prevent CSRF attacks by raising an exception. 3 | # For APIs, you may want to use :null_session instead. 4 | protect_from_forgery with: :exception 5 | end 6 | -------------------------------------------------------------------------------- /spec/dummy/app/controllers/categories_controller.rb: -------------------------------------------------------------------------------- 1 | class CategoriesController < ApplicationController 2 | before_action :set_category, only: [:show, :edit, :update, :destroy] 3 | 4 | # GET /categories 5 | def index 6 | @categories = Category.all 7 | end 8 | 9 | # GET /categories/1 10 | def show 11 | end 12 | 13 | # GET /categories/new 14 | def new 15 | @category = Category.new 16 | @form = CategoryForm.new(category: @category) 17 | end 18 | 19 | # GET /categories/1/edit 20 | def edit 21 | @form = CategoryForm.new(category: @category) 22 | end 23 | 24 | # POST /categories 25 | def create 26 | @category = Category.new(category_params) 27 | @form = CategoryForm.new(category: @category) 28 | 29 | @form.fill_attributes(category_params) 30 | if @form.save 31 | redirect_to @form, notice: 'Category was successfully created.' 32 | else 33 | render action: 'new' 34 | end 35 | end 36 | 37 | # PATCH/PUT /categories/1 38 | def update 39 | @form = CategoryForm.new(category: @category) 40 | @form.fill_attributes(category_params) 41 | if @form.save 42 | redirect_to @form, notice: 'Category was successfully updated.' 43 | else 44 | render action: 'edit' 45 | end 46 | end 47 | 48 | # DELETE /categories/1 49 | def destroy 50 | @category.destroy 51 | redirect_to categories_url, notice: 'Category was successfully destroyed.' 52 | end 53 | 54 | private 55 | # Use callbacks to share common setup or constraints between actions. 56 | def set_category 57 | @category = Category.find(params[:id]) 58 | end 59 | 60 | # Only allow a trusted parameter "white list" through. 61 | def category_params 62 | params.require(:category).permit(:title, user_ids: []) 63 | end 64 | end 65 | -------------------------------------------------------------------------------- /spec/dummy/app/controllers/concerns/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GCorbel/activeform-rails/d907b7dd4c9fdffe79b0c08d5a54baa86b415b75/spec/dummy/app/controllers/concerns/.keep -------------------------------------------------------------------------------- /spec/dummy/app/controllers/users_controller.rb: -------------------------------------------------------------------------------- 1 | class UsersController < ApplicationController 2 | before_action :set_user, only: [:show, :edit, :update, :destroy] 3 | 4 | # GET /users 5 | def index 6 | @users = User.all 7 | end 8 | 9 | # GET /users/1 10 | def show 11 | end 12 | 13 | # GET /users/new 14 | def new 15 | @form = UserForm.new(user: User.new) 16 | end 17 | 18 | # GET /users/1/edit 19 | def edit 20 | @form = UserForm.new(user: @user) 21 | end 22 | 23 | # POST /users 24 | def create 25 | @user = User.new(user_params) 26 | @form = UserForm.new(user: @user) 27 | @form.fill_attributes(user_params) 28 | 29 | if @form.valid? 30 | @form.save 31 | redirect_to @user, notice: 'User was successfully created.' 32 | else 33 | render action: 'new' 34 | end 35 | end 36 | 37 | # PATCH/PUT /users/1 38 | def update 39 | @form = UserForm.new(user: @user) 40 | @form.fill_attributes(user_params) 41 | 42 | if @form.valid? 43 | @form.save 44 | redirect_to @user, notice: 'User was successfully updated.' 45 | else 46 | render action: 'edit' 47 | end 48 | end 49 | 50 | # DELETE /users/1 51 | def destroy 52 | @user.destroy 53 | redirect_to users_url, notice: 'User was successfully destroyed.' 54 | end 55 | 56 | private 57 | # Use callbacks to share common setup or constraints between actions. 58 | def set_user 59 | @user = User.find(params[:id]) 60 | end 61 | 62 | # Only allow a trusted parameter "white list" through. 63 | def user_params 64 | params.require(:user).permit(:name, :category_id) 65 | end 66 | end 67 | -------------------------------------------------------------------------------- /spec/dummy/app/forms/category_form.rb: -------------------------------------------------------------------------------- 1 | class CategoryForm 2 | include ActiveForm::Form 3 | 4 | properties :title, on: :category 5 | 6 | self.main_model = :category 7 | 8 | validates :title, presence: true 9 | 10 | attr_accessor :user_ids 11 | 12 | def fill_attributes(attributes) 13 | super(attributes) 14 | end 15 | 16 | def save 17 | super do 18 | category.save 19 | category.users = user_ids.delete_if(&:empty?).map do |user_id| 20 | User.find(user_id) 21 | end 22 | end 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /spec/dummy/app/forms/user_form.rb: -------------------------------------------------------------------------------- 1 | class UserForm 2 | include ActiveForm::Form 3 | include ActiveForm::ValidateUniqueness 4 | 5 | properties :name, :category_id, :category, on: :user 6 | 7 | validates_uniqueness_of :name, :user 8 | 9 | self.main_model = :user 10 | 11 | validates :name, presence: true 12 | end 13 | -------------------------------------------------------------------------------- /spec/dummy/app/helpers/application_helper.rb: -------------------------------------------------------------------------------- 1 | module ApplicationHelper 2 | end 3 | -------------------------------------------------------------------------------- /spec/dummy/app/mailers/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GCorbel/activeform-rails/d907b7dd4c9fdffe79b0c08d5a54baa86b415b75/spec/dummy/app/mailers/.keep -------------------------------------------------------------------------------- /spec/dummy/app/models/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GCorbel/activeform-rails/d907b7dd4c9fdffe79b0c08d5a54baa86b415b75/spec/dummy/app/models/.keep -------------------------------------------------------------------------------- /spec/dummy/app/models/category.rb: -------------------------------------------------------------------------------- 1 | class Category < ActiveRecord::Base 2 | has_many :users 3 | end 4 | -------------------------------------------------------------------------------- /spec/dummy/app/models/concerns/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GCorbel/activeform-rails/d907b7dd4c9fdffe79b0c08d5a54baa86b415b75/spec/dummy/app/models/concerns/.keep -------------------------------------------------------------------------------- /spec/dummy/app/models/user.rb: -------------------------------------------------------------------------------- 1 | class User < ActiveRecord::Base 2 | belongs_to :category 3 | end 4 | -------------------------------------------------------------------------------- /spec/dummy/app/views/categories/_form.html.erb: -------------------------------------------------------------------------------- 1 | <%= simple_form_for(@form) do |f| %> 2 | <%= f.input :title %> 3 | <%= f.association :users %> 4 | <%= f.submit %> 5 | <% end %> 6 | -------------------------------------------------------------------------------- /spec/dummy/app/views/categories/edit.html.erb: -------------------------------------------------------------------------------- 1 |

Editing category

2 | 3 | <%= render 'form' %> 4 | 5 | <%= link_to 'Show', @category %> | 6 | <%= link_to 'Back', categories_path %> 7 | -------------------------------------------------------------------------------- /spec/dummy/app/views/categories/index.html.erb: -------------------------------------------------------------------------------- 1 |

Listing categories

2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | <% @categories.each do |category| %> 15 | 16 | 17 | 18 | 19 | 20 | 21 | <% end %> 22 | 23 |
Title
<%= category.title %><%= link_to 'Show', category %><%= link_to 'Edit', edit_category_path(category) %><%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %>
24 | 25 |
26 | 27 | <%= link_to 'New Category', new_category_path %> 28 | -------------------------------------------------------------------------------- /spec/dummy/app/views/categories/new.html.erb: -------------------------------------------------------------------------------- 1 |

New category

2 | 3 | <%= render 'form' %> 4 | 5 | <%= link_to 'Back', categories_path %> 6 | -------------------------------------------------------------------------------- /spec/dummy/app/views/categories/show.html.erb: -------------------------------------------------------------------------------- 1 |

<%= notice %>

2 | 3 |

4 | Title: 5 | <%= @category.title %> 6 |

7 | 8 | <%= link_to 'Edit', edit_category_path(@category) %> | 9 | <%= link_to 'Back', categories_path %> 10 | -------------------------------------------------------------------------------- /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 %> 7 | <%= csrf_meta_tags %> 8 | 9 | 10 | 11 | <%= yield %> 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /spec/dummy/app/views/users/_form.html.erb: -------------------------------------------------------------------------------- 1 | <%= simple_form_for(@form) do |f| %> 2 | <%= f.input :name %> 3 | <%= f.association :category %> 4 | <%= f.submit %> 5 | <% end %> 6 | -------------------------------------------------------------------------------- /spec/dummy/app/views/users/edit.html.erb: -------------------------------------------------------------------------------- 1 |

Editing user

2 | 3 | <%= render 'form' %> 4 | 5 | <%= link_to 'Show', @user %> | 6 | <%= link_to 'Back', users_path %> 7 | -------------------------------------------------------------------------------- /spec/dummy/app/views/users/index.html.erb: -------------------------------------------------------------------------------- 1 |

Listing users

2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | <% @users.each do |user| %> 15 | 16 | 17 | 18 | 19 | 20 | 21 | <% end %> 22 | 23 |
Name
<%= user.name %><%= link_to 'Show', user %><%= link_to 'Edit', edit_user_path(user) %><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %>
24 | 25 |
26 | 27 | <%= link_to 'New User', new_user_path %> 28 | -------------------------------------------------------------------------------- /spec/dummy/app/views/users/new.html.erb: -------------------------------------------------------------------------------- 1 |

New user

2 | 3 | <%= render 'form' %> 4 | 5 | <%= link_to 'Back', users_path %> 6 | -------------------------------------------------------------------------------- /spec/dummy/app/views/users/show.html.erb: -------------------------------------------------------------------------------- 1 |

<%= notice %>

2 | 3 |

4 | Name: 5 | <%= @user.name %> 6 |

7 | 8 | <%= link_to 'Edit', edit_user_path(@user) %> | 9 | <%= link_to 'Back', users_path %> 10 | -------------------------------------------------------------------------------- /spec/dummy/bin/bundle: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) 3 | load Gem.bin_path('bundler', 'bundle') 4 | -------------------------------------------------------------------------------- /spec/dummy/bin/rails: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | APP_PATH = File.expand_path('../../config/application', __FILE__) 3 | require_relative '../config/boot' 4 | require 'rails/commands' 5 | -------------------------------------------------------------------------------- /spec/dummy/bin/rake: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require_relative '../config/boot' 3 | require 'rake' 4 | Rake.application.run 5 | -------------------------------------------------------------------------------- /spec/dummy/config.ru: -------------------------------------------------------------------------------- 1 | # This file is used by Rack-based servers to start the application. 2 | 3 | require ::File.expand_path('../config/environment', __FILE__) 4 | run Rails.application 5 | -------------------------------------------------------------------------------- /spec/dummy/config/application.rb: -------------------------------------------------------------------------------- 1 | require File.expand_path('../boot', __FILE__) 2 | 3 | require 'rails/all' 4 | 5 | Bundler.require(*Rails.groups) 6 | require "activeform-rails" 7 | 8 | module Dummy 9 | class Application < Rails::Application 10 | # Settings in config/environments/* take precedence over those specified here. 11 | # Application configuration should go into files in config/initializers 12 | # -- all .rb files in that directory are automatically loaded. 13 | 14 | # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. 15 | # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. 16 | # config.time_zone = 'Central Time (US & Canada)' 17 | 18 | # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. 19 | # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] 20 | # config.i18n.default_locale = :de 21 | end 22 | end 23 | 24 | -------------------------------------------------------------------------------- /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.exists?(ENV['BUNDLE_GEMFILE']) 5 | $LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__) 6 | -------------------------------------------------------------------------------- /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 | development: 7 | adapter: sqlite3 8 | database: db/development.sqlite3 9 | pool: 5 10 | timeout: 5000 11 | 12 | # Warning: The database defined as "test" will be erased and 13 | # re-generated from your development database when you run "rake". 14 | # Do not set this db to the same as development or production. 15 | test: 16 | adapter: sqlite3 17 | database: db/test.sqlite3 18 | pool: 5 19 | timeout: 5000 20 | 21 | production: 22 | adapter: sqlite3 23 | database: db/production.sqlite3 24 | pool: 5 25 | timeout: 5000 26 | -------------------------------------------------------------------------------- /spec/dummy/config/environment.rb: -------------------------------------------------------------------------------- 1 | # Load the Rails application. 2 | require File.expand_path('../application', __FILE__) 3 | 4 | # Initialize the Rails application. 5 | Dummy::Application.initialize! 6 | -------------------------------------------------------------------------------- /spec/dummy/config/environments/development.rb: -------------------------------------------------------------------------------- 1 | Dummy::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 | end 30 | -------------------------------------------------------------------------------- /spec/dummy/config/environments/production.rb: -------------------------------------------------------------------------------- 1 | Dummy::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 thread 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 nginx, varnish or squid. 20 | # config.action_dispatch.rack_cache = true 21 | 22 | # Disable Rails's static asset server (Apache or nginx will already do this). 23 | config.serve_static_assets = false 24 | 25 | # Compress JavaScripts and CSS. 26 | config.assets.js_compressor = :uglifier 27 | # config.assets.css_compressor = :sass 28 | 29 | # Do not fallback to assets pipeline if a precompiled asset is missed. 30 | config.assets.compile = false 31 | 32 | # Generate digests for assets URLs. 33 | config.assets.digest = true 34 | 35 | # Version of your assets, change this if you want to expire all your assets. 36 | config.assets.version = '1.0' 37 | 38 | # Specifies the header that your server uses for sending files. 39 | # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache 40 | # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx 41 | 42 | # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. 43 | # config.force_ssl = true 44 | 45 | # Set to :debug to see everything in the log. 46 | config.log_level = :info 47 | 48 | # Prepend all log lines with the following tags. 49 | # config.log_tags = [ :subdomain, :uuid ] 50 | 51 | # Use a different logger for distributed setups. 52 | # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new) 53 | 54 | # Use a different cache store in production. 55 | # config.cache_store = :mem_cache_store 56 | 57 | # Enable serving of images, stylesheets, and JavaScripts from an asset server. 58 | # config.action_controller.asset_host = "http://assets.example.com" 59 | 60 | # Precompile additional assets. 61 | # application.js, application.css, and all non-JS/CSS in app/assets folder are already added. 62 | # config.assets.precompile += %w( search.js ) 63 | 64 | # Ignore bad email addresses and do not raise email delivery errors. 65 | # Set this to true and configure the email server for immediate delivery to raise delivery errors. 66 | # config.action_mailer.raise_delivery_errors = false 67 | 68 | # Enable locale fallbacks for I18n (makes lookups for any locale fall back to 69 | # the I18n.default_locale when a translation can not be found). 70 | config.i18n.fallbacks = true 71 | 72 | # Send deprecation notices to registered listeners. 73 | config.active_support.deprecation = :notify 74 | 75 | # Disable automatic flushing of the log to improve performance. 76 | # config.autoflush_log = false 77 | 78 | # Use default logging formatter so that PID and timestamp are not suppressed. 79 | config.log_formatter = ::Logger::Formatter.new 80 | end 81 | -------------------------------------------------------------------------------- /spec/dummy/config/environments/test.rb: -------------------------------------------------------------------------------- 1 | Dummy::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 | 10 | # Do not eager load code on boot. This avoids loading your whole application 11 | # just for the purpose of running a single test. If you are using a tool that 12 | # preloads Rails for running tests, you may have to set it to true. 13 | config.eager_load = false 14 | 15 | # Configure static asset server for tests with Cache-Control for performance. 16 | config.serve_static_assets = true 17 | config.static_cache_control = "public, max-age=3600" 18 | 19 | # Show full error reports and disable caching. 20 | config.consider_all_requests_local = true 21 | config.action_controller.perform_caching = false 22 | 23 | # Raise exceptions instead of rendering exception templates. 24 | config.action_dispatch.show_exceptions = false 25 | 26 | # Disable request forgery protection in test environment. 27 | config.action_controller.allow_forgery_protection = false 28 | 29 | # Tell Action Mailer not to deliver emails to the real world. 30 | # The :test delivery method accumulates sent emails in the 31 | # ActionMailer::Base.deliveries array. 32 | config.action_mailer.delivery_method = :test 33 | 34 | # Print deprecation notices to the stderr. 35 | config.active_support.deprecation = :stderr 36 | end 37 | -------------------------------------------------------------------------------- /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/filter_parameter_logging.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Configure sensitive parameters which will be filtered from the log file. 4 | Rails.application.config.filter_parameters += [:password] 5 | -------------------------------------------------------------------------------- /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/initializers/mime_types.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Add new mime types for use in respond_to blocks: 4 | # Mime::Type.register "text/richtext", :rtf 5 | # Mime::Type.register_alias "text/html", :iphone 6 | -------------------------------------------------------------------------------- /spec/dummy/config/initializers/secret_token.rb: -------------------------------------------------------------------------------- 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 your secret_key_base is kept private 11 | # if you're sharing your code publicly. 12 | Dummy::Application.config.secret_key_base = '986c27f80dc64819a76f41beed3d85ed9613413f4cfd4ff7f45bc08f859f5bd9c07c1183d4387c4a7d3a2b2f3a7d5f42dbac29611e05d7efe5b67ddcbd44dd4b' 13 | -------------------------------------------------------------------------------- /spec/dummy/config/initializers/session_store.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | Dummy::Application.config.session_store :cookie_store, key: '_dummy_session' 4 | -------------------------------------------------------------------------------- /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/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/config/routes.rb: -------------------------------------------------------------------------------- 1 | Dummy::Application.routes.draw do 2 | resources :categories 3 | resources :users 4 | 5 | root 'users#index' 6 | 7 | # The priority is based upon order of creation: first created -> highest priority. 8 | # See how all your routes lay out with "rake routes". 9 | 10 | # You can have the root of your site routed with "root" 11 | # root 'welcome#index' 12 | 13 | # Example of regular route: 14 | # get 'products/:id' => 'catalog#view' 15 | 16 | # Example of named route that can be invoked with purchase_url(id: product.id) 17 | # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase 18 | 19 | # Example resource route (maps HTTP verbs to controller actions automatically): 20 | # resources :products 21 | 22 | # Example resource route with options: 23 | # resources :products do 24 | # member do 25 | # get 'short' 26 | # post 'toggle' 27 | # end 28 | # 29 | # collection do 30 | # get 'sold' 31 | # end 32 | # end 33 | 34 | # Example resource route with sub-resources: 35 | # resources :products do 36 | # resources :comments, :sales 37 | # resource :seller 38 | # end 39 | 40 | # Example resource route with more complex sub-resources: 41 | # resources :products do 42 | # resources :comments 43 | # resources :sales do 44 | # get 'recent', on: :collection 45 | # end 46 | # end 47 | 48 | # Example resource route with concerns: 49 | # concern :toggleable do 50 | # post 'toggle' 51 | # end 52 | # resources :posts, concerns: :toggleable 53 | # resources :photos, concerns: :toggleable 54 | 55 | # Example resource route within a namespace: 56 | # namespace :admin do 57 | # # Directs /admin/products/* to Admin::ProductsController 58 | # # (app/controllers/admin/products_controller.rb) 59 | # resources :products 60 | # end 61 | end 62 | -------------------------------------------------------------------------------- /spec/dummy/db/development.sqlite3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GCorbel/activeform-rails/d907b7dd4c9fdffe79b0c08d5a54baa86b415b75/spec/dummy/db/development.sqlite3 -------------------------------------------------------------------------------- /spec/dummy/db/migrate/20140216201540_create_users.rb: -------------------------------------------------------------------------------- 1 | class CreateUsers < ActiveRecord::Migration 2 | def change 3 | create_table :users do |t| 4 | t.string :name 5 | t.belongs_to :category, index: true 6 | 7 | t.timestamps 8 | end 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /spec/dummy/db/migrate/20140222122256_create_categories.rb: -------------------------------------------------------------------------------- 1 | class CreateCategories < ActiveRecord::Migration 2 | def change 3 | create_table :categories do |t| 4 | t.string :title 5 | 6 | t.timestamps 7 | end 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /spec/dummy/db/schema.rb: -------------------------------------------------------------------------------- 1 | # encoding: UTF-8 2 | # This file is auto-generated from the current state of the database. Instead 3 | # of editing this file, please use the migrations feature of Active Record to 4 | # incrementally modify your database, and then regenerate this schema definition. 5 | # 6 | # Note that this schema.rb definition is the authoritative source for your 7 | # database schema. If you need to create the application database on another 8 | # system, you should be using db:schema:load, not running all the migrations 9 | # from scratch. The latter is a flawed and unsustainable approach (the more migrations 10 | # you'll amass, the slower it'll run and the greater likelihood for issues). 11 | # 12 | # It's strongly recommended that you check this file into your version control system. 13 | 14 | ActiveRecord::Schema.define(version: 20140222122256) do 15 | 16 | create_table "categories", force: true do |t| 17 | t.string "title" 18 | t.datetime "created_at" 19 | t.datetime "updated_at" 20 | end 21 | 22 | create_table "users", force: true do |t| 23 | t.string "name" 24 | t.integer "category_id" 25 | t.datetime "created_at" 26 | t.datetime "updated_at" 27 | end 28 | 29 | add_index "users", ["category_id"], name: "index_users_on_category_id" 30 | 31 | end 32 | -------------------------------------------------------------------------------- /spec/dummy/lib/assets/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GCorbel/activeform-rails/d907b7dd4c9fdffe79b0c08d5a54baa86b415b75/spec/dummy/lib/assets/.keep -------------------------------------------------------------------------------- /spec/dummy/log/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GCorbel/activeform-rails/d907b7dd4c9fdffe79b0c08d5a54baa86b415b75/spec/dummy/log/.keep -------------------------------------------------------------------------------- /spec/dummy/public/404.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | The page you were looking for doesn't exist (404) 5 | 48 | 49 | 50 | 51 | 52 |
53 |

The page you were looking for doesn't exist.

54 |

You may have mistyped the address or the page may have moved.

55 |
56 |

If you are the application owner check the logs for more information.

57 | 58 | 59 | -------------------------------------------------------------------------------- /spec/dummy/public/422.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | The change you wanted was rejected (422) 5 | 48 | 49 | 50 | 51 | 52 |
53 |

The change you wanted was rejected.

54 |

Maybe you tried to change something you didn't have access to.

55 |
56 |

If you are the application owner check the logs for more information.

57 | 58 | 59 | -------------------------------------------------------------------------------- /spec/dummy/public/500.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | We're sorry, but something went wrong (500) 5 | 48 | 49 | 50 | 51 | 52 |
53 |

We're sorry, but something went wrong.

54 |
55 |

If you are the application owner check the logs for more information.

56 | 57 | 58 | -------------------------------------------------------------------------------- /spec/dummy/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GCorbel/activeform-rails/d907b7dd4c9fdffe79b0c08d5a54baa86b415b75/spec/dummy/public/favicon.ico -------------------------------------------------------------------------------- /spec/dummy/test/controllers/categories_controller_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class CategoriesControllerTest < ActionController::TestCase 4 | setup do 5 | @category = categories(:one) 6 | end 7 | 8 | test "should get index" do 9 | get :index 10 | assert_response :success 11 | assert_not_nil assigns(:categories) 12 | end 13 | 14 | test "should get new" do 15 | get :new 16 | assert_response :success 17 | end 18 | 19 | test "should create category" do 20 | assert_difference('Category.count') do 21 | post :create, category: { title: @category.title } 22 | end 23 | 24 | assert_redirected_to category_path(assigns(:category)) 25 | end 26 | 27 | test "should show category" do 28 | get :show, id: @category 29 | assert_response :success 30 | end 31 | 32 | test "should get edit" do 33 | get :edit, id: @category 34 | assert_response :success 35 | end 36 | 37 | test "should update category" do 38 | patch :update, id: @category, category: { title: @category.title } 39 | assert_redirected_to category_path(assigns(:category)) 40 | end 41 | 42 | test "should destroy category" do 43 | assert_difference('Category.count', -1) do 44 | delete :destroy, id: @category 45 | end 46 | 47 | assert_redirected_to categories_path 48 | end 49 | end 50 | -------------------------------------------------------------------------------- /spec/dummy/test/controllers/users_controller_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class UsersControllerTest < ActionController::TestCase 4 | setup do 5 | @user = users(:one) 6 | end 7 | 8 | test "should get index" do 9 | get :index 10 | assert_response :success 11 | assert_not_nil assigns(:users) 12 | end 13 | 14 | test "should get new" do 15 | get :new 16 | assert_response :success 17 | end 18 | 19 | test "should create user" do 20 | assert_difference('User.count') do 21 | post :create, user: { name: @user.name } 22 | end 23 | 24 | assert_redirected_to user_path(assigns(:user)) 25 | end 26 | 27 | test "should show user" do 28 | get :show, id: @user 29 | assert_response :success 30 | end 31 | 32 | test "should get edit" do 33 | get :edit, id: @user 34 | assert_response :success 35 | end 36 | 37 | test "should update user" do 38 | patch :update, id: @user, user: { name: @user.name } 39 | assert_redirected_to user_path(assigns(:user)) 40 | end 41 | 42 | test "should destroy user" do 43 | assert_difference('User.count', -1) do 44 | delete :destroy, id: @user 45 | end 46 | 47 | assert_redirected_to users_path 48 | end 49 | end 50 | -------------------------------------------------------------------------------- /spec/dummy/test/fixtures/categories.yml: -------------------------------------------------------------------------------- 1 | # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html 2 | 3 | one: 4 | title: MyString 5 | 6 | two: 7 | title: MyString 8 | -------------------------------------------------------------------------------- /spec/dummy/test/fixtures/users.yml: -------------------------------------------------------------------------------- 1 | # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html 2 | 3 | one: 4 | name: MyString 5 | 6 | two: 7 | name: MyString 8 | -------------------------------------------------------------------------------- /spec/dummy/test/helpers/categories_helper_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class CategoriesHelperTest < ActionView::TestCase 4 | end 5 | -------------------------------------------------------------------------------- /spec/dummy/test/helpers/users_helper_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class UsersHelperTest < ActionView::TestCase 4 | end 5 | -------------------------------------------------------------------------------- /spec/dummy/test/models/category_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class CategoryTest < ActiveSupport::TestCase 4 | # test "the truth" do 5 | # assert true 6 | # end 7 | end 8 | -------------------------------------------------------------------------------- /spec/dummy/test/models/user_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class UserTest < ActiveSupport::TestCase 4 | # test "the truth" do 5 | # assert true 6 | # end 7 | end 8 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'active_record' 2 | require 'activeform-rails' 3 | require 'database_cleaner' 4 | 5 | ActiveRecord::Base.establish_connection( 6 | :adapter => "sqlite3", 7 | :database => ':memory:' 8 | ) 9 | 10 | ['CREATE TABLE categories(id INTEGER PRIMARY KEY, title, user_id)', 11 | 'CREATE TABLE users(id INTEGER PRIMARY KEY, name, firstname)'].each do |sql| 12 | ActiveRecord::Base.connection.execute sql 13 | end 14 | 15 | class User < ActiveRecord::Base 16 | has_many :categories 17 | end 18 | 19 | class Category < ActiveRecord::Base 20 | belongs_to :user 21 | end 22 | 23 | RSpec.configure do |config| 24 | config.before(:suite) do 25 | DatabaseCleaner.strategy = :transaction 26 | DatabaseCleaner.clean_with(:truncation) 27 | end 28 | 29 | config.before(:each) do 30 | DatabaseCleaner.start 31 | end 32 | 33 | config.after(:each) do 34 | DatabaseCleaner.clean 35 | end 36 | end 37 | 38 | # hide deprecation warnings, see http://stackoverflow.com/questions/20361428/rails-i18n-validation-deprecation-warning 39 | I18n.config.enforce_available_locales = true 40 | 41 | --------------------------------------------------------------------------------