activeform-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +32 -0
  4. data/lib/activeform.rb +6 -0
  5. data/lib/activeform/form.rb +93 -0
  6. data/lib/activeform/validate_uniqueness.rb +44 -0
  7. data/lib/activeform/version.rb +3 -0
  8. data/lib/tasks/activeform_tasks.rake +4 -0
  9. data/spec/activeform/form_spec.rb +124 -0
  10. data/spec/activeform/validate_uniqueness_spec.rb +37 -0
  11. data/spec/dummy/Gemfile +7 -0
  12. data/spec/dummy/Gemfile.lock +94 -0
  13. data/spec/dummy/README.rdoc +28 -0
  14. data/spec/dummy/Rakefile +6 -0
  15. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  16. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  17. data/spec/dummy/app/assets/stylesheets/scaffold.css +56 -0
  18. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  19. data/spec/dummy/app/controllers/categories_controller.rb +64 -0
  20. data/spec/dummy/app/controllers/users_controller.rb +66 -0
  21. data/spec/dummy/app/forms/category_form.rb +24 -0
  22. data/spec/dummy/app/forms/user_form.rb +12 -0
  23. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  24. data/spec/dummy/app/models/category.rb +3 -0
  25. data/spec/dummy/app/models/user.rb +3 -0
  26. data/spec/dummy/app/views/categories/_form.html.erb +5 -0
  27. data/spec/dummy/app/views/categories/edit.html.erb +6 -0
  28. data/spec/dummy/app/views/categories/index.html.erb +27 -0
  29. data/spec/dummy/app/views/categories/new.html.erb +5 -0
  30. data/spec/dummy/app/views/categories/show.html.erb +9 -0
  31. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  32. data/spec/dummy/app/views/users/_form.html.erb +5 -0
  33. data/spec/dummy/app/views/users/edit.html.erb +6 -0
  34. data/spec/dummy/app/views/users/index.html.erb +27 -0
  35. data/spec/dummy/app/views/users/new.html.erb +5 -0
  36. data/spec/dummy/app/views/users/show.html.erb +9 -0
  37. data/spec/dummy/bin/bundle +3 -0
  38. data/spec/dummy/bin/rails +4 -0
  39. data/spec/dummy/bin/rake +4 -0
  40. data/spec/dummy/config.ru +4 -0
  41. data/spec/dummy/config/application.rb +23 -0
  42. data/spec/dummy/config/boot.rb +5 -0
  43. data/spec/dummy/config/database.yml +25 -0
  44. data/spec/dummy/config/environment.rb +5 -0
  45. data/spec/dummy/config/environments/development.rb +29 -0
  46. data/spec/dummy/config/environments/production.rb +80 -0
  47. data/spec/dummy/config/environments/test.rb +36 -0
  48. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  49. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  50. data/spec/dummy/config/initializers/inflections.rb +16 -0
  51. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  52. data/spec/dummy/config/initializers/secret_token.rb +12 -0
  53. data/spec/dummy/config/initializers/session_store.rb +3 -0
  54. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  55. data/spec/dummy/config/locales/en.yml +23 -0
  56. data/spec/dummy/config/routes.rb +61 -0
  57. data/spec/dummy/db/development.sqlite3 +0 -0
  58. data/spec/dummy/db/migrate/20140216201540_create_users.rb +10 -0
  59. data/spec/dummy/db/migrate/20140222122256_create_categories.rb +9 -0
  60. data/spec/dummy/db/schema.rb +31 -0
  61. data/spec/dummy/public/404.html +58 -0
  62. data/spec/dummy/public/422.html +58 -0
  63. data/spec/dummy/public/500.html +57 -0
  64. data/spec/dummy/public/favicon.ico +0 -0
  65. data/spec/dummy/test/controllers/categories_controller_test.rb +49 -0
  66. data/spec/dummy/test/controllers/users_controller_test.rb +49 -0
  67. data/spec/dummy/test/fixtures/categories.yml +7 -0
  68. data/spec/dummy/test/fixtures/users.yml +7 -0
  69. data/spec/dummy/test/helpers/categories_helper_test.rb +4 -0
  70. data/spec/dummy/test/helpers/users_helper_test.rb +4 -0
  71. data/spec/dummy/test/models/category_test.rb +7 -0
  72. data/spec/dummy/test/models/user_test.rb +7 -0
  73. data/spec/spec_helper.rb +32 -0
  74. metadata +223 -0
@@ -0,0 +1,28 @@
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
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
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
@@ -0,0 +1,13 @@
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 .
@@ -0,0 +1,13 @@
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
+ */
@@ -0,0 +1,56 @@
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
+ }
@@ -0,0 +1,5 @@
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
@@ -0,0 +1,64 @@
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
@@ -0,0 +1,66 @@
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
@@ -0,0 +1,24 @@
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
@@ -0,0 +1,12 @@
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
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,3 @@
1
+ class Category < ActiveRecord::Base
2
+ has_many :users
3
+ end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ belongs_to :category
3
+ end
@@ -0,0 +1,5 @@
1
+ <%= simple_form_for(@form) do |f| %>
2
+ <%= f.input :title %>
3
+ <%= f.association :users %>
4
+ <%= f.submit %>
5
+ <% end %>
@@ -0,0 +1,6 @@
1
+ <h1>Editing category</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Show', @category %> |
6
+ <%= link_to 'Back', categories_path %>
@@ -0,0 +1,27 @@
1
+ <h1>Listing categories</h1>
2
+
3
+ <table>
4
+ <thead>
5
+ <tr>
6
+ <th>Title</th>
7
+ <th></th>
8
+ <th></th>
9
+ <th></th>
10
+ </tr>
11
+ </thead>
12
+
13
+ <tbody>
14
+ <% @categories.each do |category| %>
15
+ <tr>
16
+ <td><%= category.title %></td>
17
+ <td><%= link_to 'Show', category %></td>
18
+ <td><%= link_to 'Edit', edit_category_path(category) %></td>
19
+ <td><%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %></td>
20
+ </tr>
21
+ <% end %>
22
+ </tbody>
23
+ </table>
24
+
25
+ <br>
26
+
27
+ <%= link_to 'New Category', new_category_path %>
@@ -0,0 +1,5 @@
1
+ <h1>New category</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Back', categories_path %>
@@ -0,0 +1,9 @@
1
+ <p id="notice"><%= notice %></p>
2
+
3
+ <p>
4
+ <strong>Title:</strong>
5
+ <%= @category.title %>
6
+ </p>
7
+
8
+ <%= link_to 'Edit', edit_category_path(@category) %> |
9
+ <%= link_to 'Back', categories_path %>
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
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
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,5 @@
1
+ <%= simple_form_for(@form) do |f| %>
2
+ <%= f.input :name %>
3
+ <%= f.association :category %>
4
+ <%= f.submit %>
5
+ <% end %>
@@ -0,0 +1,6 @@
1
+ <h1>Editing user</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Show', @user %> |
6
+ <%= link_to 'Back', users_path %>
@@ -0,0 +1,27 @@
1
+ <h1>Listing users</h1>
2
+
3
+ <table>
4
+ <thead>
5
+ <tr>
6
+ <th>Name</th>
7
+ <th></th>
8
+ <th></th>
9
+ <th></th>
10
+ </tr>
11
+ </thead>
12
+
13
+ <tbody>
14
+ <% @users.each do |user| %>
15
+ <tr>
16
+ <td><%= user.name %></td>
17
+ <td><%= link_to 'Show', user %></td>
18
+ <td><%= link_to 'Edit', edit_user_path(user) %></td>
19
+ <td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
20
+ </tr>
21
+ <% end %>
22
+ </tbody>
23
+ </table>
24
+
25
+ <br>
26
+
27
+ <%= link_to 'New User', new_user_path %>
@@ -0,0 +1,5 @@
1
+ <h1>New user</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Back', users_path %>
@@ -0,0 +1,9 @@
1
+ <p id="notice"><%= notice %></p>
2
+
3
+ <p>
4
+ <strong>Name:</strong>
5
+ <%= @user.name %>
6
+ </p>
7
+
8
+ <%= link_to 'Edit', edit_user_path(@user) %> |
9
+ <%= link_to 'Back', users_path %>