record_collection 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f3fe45579a3c799ef4243d149102f0a79bb47378
4
- data.tar.gz: ba95029f191c2412f2a0489418acd0d8735c974c
3
+ metadata.gz: 3a59adda39d602d7f6bfb8f52d621249aa2abcc4
4
+ data.tar.gz: d1944bc51f5c50f874af1e2c75ec449986a77e45
5
5
  SHA512:
6
- metadata.gz: 6234774ce4b5adfe5fc245e11f7331aa2c48ed38cc6e7e5ab21b6f38898754f22691a82ccba789c70ae4f09b58a73617be460b1ac47b425b8768549b892ee443
7
- data.tar.gz: 0e72b14a8c8b3cb8ec56872cb2ce0b856220678dd63d37a9797652e030120fbbb8dc9a4c467365d283b5c71f6d251fd88efc00d49fd5847aea6caf24562a6454
6
+ metadata.gz: d65cd419233c8bf56f0b1d447dcc02e774e967eaecd8f9cda0d350453faee3ea45b37c2012c0666e04ff67a2da9aaff0cce402d105ba2d88270b60da1ad56cf2
7
+ data.tar.gz: d1656f30b70811c497236c87300fccfd2a367c646aee2c42566deace1934c9f7fc3821675e0c06525e2bc32e33d8cb40a9ee8e95b55fa11496971b586fb4bf03
@@ -44,6 +44,7 @@ class Optionals
44
44
 
45
45
  value_toggle = $('<span></span>').addClass('optional-boolean-toggle').click ->
46
46
  return if container.hasClass('inactive')
47
+ return if container.data('disabled')
47
48
  if $(@).hasClass('active')
48
49
  value_field.val(0)
49
50
  $(@).removeClass('active').addClass('inactive')
@@ -37,6 +37,12 @@ $unchecked-color: #999
37
37
  &.inactive
38
38
  color: $unchecked-color
39
39
  @extend .fa, .fa-2x, .fa-square-o
40
+ &.disabled
41
+ .optional-boolean-toggle
42
+ &.active
43
+ color: $unchecked-color
44
+ &.inactive
45
+ color: $unchecked-color
40
46
  &.inactive
41
47
  display: block
42
48
  .optional-boolean-activator-toggle
@@ -105,6 +105,10 @@ module RecordCollection
105
105
  false
106
106
  end
107
107
 
108
+ def to_ary(*)
109
+ self
110
+ end
111
+
108
112
  # This method returns nil when the values of `attr` in the collection
109
113
  # are mixed. Otherwise the value itself. For boolean attributes the
110
114
  # check is wether the values are truthy or falsy. If the
@@ -15,36 +15,38 @@ class ActionView::Helpers::FormBuilder
15
15
  object.map{|record| @template.hidden_field_tag('ids[]', record.id, id: nil) }.join.html_safe
16
16
  end
17
17
 
18
- def optional_boolean(attr)
19
- classes = get_optional_classes(attr, base_class: 'optional-boolean')
18
+ def optional_boolean(attr, options = {})
19
+ classes = get_optional_classes(attr, options.merge(base_class: 'optional-boolean'))
20
20
  label_tag = label(attr, options[:label])
21
21
  check_box_tag = check_box(attr, options)
22
- add_collection_ids @template.content_tag(:div, label_tag + check_box_tag , class: classes, data: get_data_attributes(attr))
22
+ add_collection_ids @template.content_tag(:div, label_tag + check_box_tag , class: classes, data: get_data_attributes(attr, options))
23
23
  end
24
24
 
25
- def optional_check_box(attr)
26
- optional_boolean(attr)
25
+ def optional_check_box(attr, options = {})
26
+ optional_boolean(attr, options)
27
27
  end
28
28
 
29
29
  def optional_input(attr, options = {})
30
- classes = get_optional_classes(attr, base_class: 'optional-input')
30
+ classes = get_optional_classes(attr, options.reverse_merge(base_class: 'optional-input'))
31
31
  add_collection_ids @template.content_tag(:div, input(attr, options), class: classes, data: get_data_attributes(attr))
32
32
  end
33
33
 
34
34
  def optional_text_field(attr, options={})
35
- classes = get_optional_classes(attr, base_class: 'optional-text-field')
35
+ classes = get_optional_classes(attr, options.reverse_merge(base_class: 'optional-text-field'))
36
36
  add_collection_ids @template.content_tag(:div, label(attr, options[:label]) + text_field(attr, options), class: classes, data: get_data_attributes(attr))
37
37
  end
38
38
 
39
39
  def optional_text_area(attr, options={})
40
- classes = get_optional_classes(attr, base_class: 'optional-text-area')
40
+ classes = get_optional_classes(attr, options.reverse_merge(base_class: 'optional-text-area'))
41
41
  add_collection_ids @template.content_tag(:div, label(attr, options[:label]) + text_area(attr, options), class: classes, data: get_data_attributes(attr))
42
42
  end
43
43
 
44
44
  def get_data_attributes(attr, options = {})
45
45
  one = true
46
46
  one = object.one? if object.is_a? RecordCollection::Base
47
- {attribute: attr, one: one}
47
+ attrs = {attribute: attr, one: one}
48
+ attrs[:disabled] = true if options[:disabled]
49
+ attrs
48
50
  end
49
51
 
50
52
  def get_optional_classes(attr, options = {})
@@ -57,6 +59,7 @@ class ActionView::Helpers::FormBuilder
57
59
  active = true unless object.public_send(attr).nil? # Activate if collection attribute is not nil
58
60
  classes = [options[:base_class] || 'optional-input', 'optional-attribute-container', attr]
59
61
  classes << (active ? 'active' : 'inactive')
62
+ classes << 'disabled' if options[:disabled]
60
63
  if object.is_a? RecordCollection::Base
61
64
  classes << 'one' if object.one?
62
65
  else
@@ -1,3 +1,3 @@
1
1
  module RecordCollection
2
- VERSION = "0.6.1"
2
+ VERSION = "0.7.0"
3
3
  end
@@ -81,5 +81,10 @@ RSpec.describe Employee::Collection do
81
81
  end
82
82
  end
83
83
  end
84
+
85
+ describe "array like behaviour" do
86
+ collection = described_class.new([1])
87
+ Array.wrap( collection ).should == collection
88
+ end
84
89
  end
85
90
 
@@ -68,6 +68,6 @@ class EmployeesController < ApplicationController
68
68
 
69
69
  # Only allow a trusted parameter "white list" through.
70
70
  def employee_params
71
- params.require(:employee).permit(:name, :section)
71
+ params.require(:employee).permit(:name, :section, :vegan)
72
72
  end
73
73
  end
@@ -0,0 +1,73 @@
1
+ class ProjectsController < ApplicationController
2
+ before_action :set_project, only: [:show, :edit, :update, :destroy]
3
+
4
+ # GET /projects
5
+ def index
6
+ @projects = Project.all
7
+ end
8
+
9
+ # GET /projects/1
10
+ def show
11
+ end
12
+
13
+ # GET /projects/new
14
+ def new
15
+ @project = Project.new
16
+ end
17
+
18
+ # GET /projects/1/edit
19
+ def edit
20
+ end
21
+
22
+ # POST /projects
23
+ def create
24
+ @project = Project.new(project_params)
25
+
26
+ if @project.save
27
+ redirect_to @project, notice: 'Project was successfully created.'
28
+ else
29
+ render :new
30
+ end
31
+ end
32
+
33
+ # PATCH/PUT /projects/1
34
+ def update
35
+ if @project.update(project_params)
36
+ redirect_to @project, notice: 'Project was successfully updated.'
37
+ else
38
+ render :edit
39
+ end
40
+ end
41
+
42
+ # DELETE /projects/1
43
+ def destroy
44
+ @project.destroy
45
+ redirect_to projects_url, notice: 'Project was successfully destroyed.'
46
+ end
47
+
48
+ def collection_edit
49
+ @collection = Project::Collection.find(params[:ids])
50
+ redirect_to projects_path, alert: 'No projects selected' if @collection.empty?
51
+ end
52
+
53
+ def collection_update
54
+ @collection = Project::Collection.find(params[:ids])
55
+ if @collection.update params[:collection]
56
+ redirect_to projects_path, notice: 'Collection is updated'
57
+ else
58
+ render 'collection_edit'
59
+ end
60
+ end
61
+
62
+ private
63
+
64
+ # Use callbacks to share common setup or constraints between actions.
65
+ def set_project
66
+ @project = Project.find(params[:id])
67
+ end
68
+
69
+ # Only allow a trusted parameter "white list" through.
70
+ def project_params
71
+ params.require(:project).permit(:name)
72
+ end
73
+ end
@@ -0,0 +1,3 @@
1
+ class Project::Collection < RecordCollection::Base
2
+ attribute :finished, type: Boolean
3
+ end
@@ -0,0 +1,21 @@
1
+ <%= form_for(@project) do |f| %>
2
+ <% if @project.errors.any? %>
3
+ <div id="error_explanation">
4
+ <h2><%= pluralize(@project.errors.count, "error") %> prohibited this project from being saved:</h2>
5
+
6
+ <ul>
7
+ <% @project.errors.full_messages.each do |message| %>
8
+ <li><%= message %></li>
9
+ <% end %>
10
+ </ul>
11
+ </div>
12
+ <% end %>
13
+
14
+ <div class="field">
15
+ <%= f.label :name %><br>
16
+ <%= f.text_field :name %>
17
+ </div>
18
+ <div class="actions">
19
+ <%= f.submit %>
20
+ </div>
21
+ <% end %>
@@ -0,0 +1,9 @@
1
+ h1 Edit multiple employees
2
+ = form_for @collection, url: [:collection_update, @collection] do |f|
3
+ = render 'form_errors', target: @collection
4
+ .form-inputs= f.optional_boolean :finished, disabled: true
5
+ .form-actions= f.submit
6
+ .page-actions
7
+ = link_to 'Back', employees_path
8
+
9
+
@@ -0,0 +1,6 @@
1
+ <h1>Editing Project</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Show', @project %> |
6
+ <%= link_to 'Back', projects_path %>
@@ -0,0 +1,20 @@
1
+ h1 Listing Projects
2
+ table.with-selection
3
+ thead
4
+ tr
5
+ th Name
6
+ th Finished?
7
+ th colspan=3
8
+ tbody
9
+ - @projects.each do |project|
10
+ tr data-record=project.attributes.to_json
11
+ td= project.name
12
+ td.finished= project.finished ? 'yes' : 'no'
13
+ td= link_to 'Show', project
14
+ td= link_to 'Edit', edit_project_path(project)
15
+ td= link_to 'Destroy', project, method: :delete, data: {confirm: 'Are you sure?' }
16
+ br
17
+ .page-actions
18
+ button.actions-button.button.warning onclick="window.location = Routes.collection_edit_projects_path({ids: MultiSelect.selected_ids()})" Actions
19
+ span.devider
20
+ = link_to 'New Project', new_project_path, class: 'info button'
@@ -0,0 +1,5 @@
1
+ <h1>New Project</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Back', projects_path %>
@@ -0,0 +1,14 @@
1
+ <p id="notice"><%= notice %></p>
2
+
3
+ <p>
4
+ <strong>Name:</strong>
5
+ <%= @project.name %>
6
+ </p>
7
+
8
+ <p>
9
+ <strong>Finished:</strong>
10
+ <%= @project.finished ? 'yes' : 'no' %>
11
+ </p>
12
+
13
+ <%= link_to 'Edit', edit_project_path(@project) %> |
14
+ <%= link_to 'Back', projects_path %>
@@ -1,3 +1,4 @@
1
1
  Rails.application.routes.draw do
2
2
  collection_resources :employees
3
+ collection_resources :projects
3
4
  end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.feature "Disabled booleans", type: :feature do
4
+ scenario "Selecting all", js: true do
5
+ project = Project.create name: 'P1', finished: true
6
+ visit collection_edit_projects_path(ids: [project.id])
7
+
8
+ toggle = find('.optional-attribute-container.finished .optional-boolean-toggle')
9
+ toggle['class'].should include 'active'
10
+ toggle.click
11
+ find('[name="commit"]').click
12
+
13
+ project.reload
14
+
15
+ # Should not be changed by the click
16
+ project.finished.should be true
17
+ end
18
+
19
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.feature "Optional text_field with normal record", type: :feature do
4
+ scenario "Selecting all", js: true do
5
+ employee = Employee.create name: 'E1', admin: true, vegan: false
6
+ visit collection_edit_employees_path(ids: [employee.id])
7
+
8
+ find('#collection_section').set 'SEC' # this is an optional
9
+ toggle = find('.optional-attribute-container.vegan .optional-boolean-toggle')
10
+ toggle.click
11
+ find('[name="commit"]').click
12
+
13
+ employee.reload
14
+
15
+ employee.section.should == 'SEC'
16
+ employee.admin.should be true
17
+ employee.vegan.should be true
18
+ end
19
+
20
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: record_collection
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin ter Kuile
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-28 00:00:00.000000000 Z
11
+ date: 2015-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -384,7 +384,7 @@ files:
384
384
  - record_collection.gemspec
385
385
  - spec/base/accessors_spec.rb
386
386
  - spec/base/after_record_update_spec.rb
387
- - spec/base/delegation_spec.rb
387
+ - spec/base/behaviour_spec.rb
388
388
  - spec/base/finding_records_spec.rb
389
389
  - spec/base/validations_spec.rb
390
390
  - spec/dummy/README.rdoc
@@ -401,6 +401,7 @@ files:
401
401
  - spec/dummy/app/controllers/application_controller.rb
402
402
  - spec/dummy/app/controllers/concerns/.keep
403
403
  - spec/dummy/app/controllers/employees_controller.rb
404
+ - spec/dummy/app/controllers/projects_controller.rb
404
405
  - spec/dummy/app/helpers/application_helper.rb
405
406
  - spec/dummy/app/mailers/.keep
406
407
  - spec/dummy/app/models/.keep
@@ -408,6 +409,7 @@ files:
408
409
  - spec/dummy/app/models/employee.rb
409
410
  - spec/dummy/app/models/employee/collection.rb
410
411
  - spec/dummy/app/models/project.rb
412
+ - spec/dummy/app/models/project/collection.rb
411
413
  - spec/dummy/app/views/application/_form_errors.html.slim
412
414
  - spec/dummy/app/views/employees/_form.html.erb
413
415
  - spec/dummy/app/views/employees/collection_edit.html.slim
@@ -416,6 +418,12 @@ files:
416
418
  - spec/dummy/app/views/employees/new.html.erb
417
419
  - spec/dummy/app/views/employees/show.html.erb
418
420
  - spec/dummy/app/views/layouts/application.html.slim
421
+ - spec/dummy/app/views/projects/_form.html.erb
422
+ - spec/dummy/app/views/projects/collection_edit.html.slim
423
+ - spec/dummy/app/views/projects/edit.html.erb
424
+ - spec/dummy/app/views/projects/index.html.slim
425
+ - spec/dummy/app/views/projects/new.html.erb
426
+ - spec/dummy/app/views/projects/show.html.erb
419
427
  - spec/dummy/bin/bundle
420
428
  - spec/dummy/bin/rails
421
429
  - spec/dummy/bin/rake
@@ -450,7 +458,9 @@ files:
450
458
  - spec/dummy/public/422.html
451
459
  - spec/dummy/public/500.html
452
460
  - spec/dummy/public/favicon.ico
461
+ - spec/features/disabled_boolean_spec.rb
453
462
  - spec/features/multi_select_spec.rb
463
+ - spec/features/optional_boolean_with_normal_resource_spec.rb
454
464
  - spec/features/optional_text_field_with_normal_resource_spec.rb
455
465
  - spec/features/optionals_with_one_record_spec.rb
456
466
  - spec/features/translations_spec.rb
@@ -485,7 +495,7 @@ summary: Manage collections of records in Ruby on Rails
485
495
  test_files:
486
496
  - spec/base/accessors_spec.rb
487
497
  - spec/base/after_record_update_spec.rb
488
- - spec/base/delegation_spec.rb
498
+ - spec/base/behaviour_spec.rb
489
499
  - spec/base/finding_records_spec.rb
490
500
  - spec/base/validations_spec.rb
491
501
  - spec/dummy/README.rdoc
@@ -502,6 +512,7 @@ test_files:
502
512
  - spec/dummy/app/controllers/application_controller.rb
503
513
  - spec/dummy/app/controllers/concerns/.keep
504
514
  - spec/dummy/app/controllers/employees_controller.rb
515
+ - spec/dummy/app/controllers/projects_controller.rb
505
516
  - spec/dummy/app/helpers/application_helper.rb
506
517
  - spec/dummy/app/mailers/.keep
507
518
  - spec/dummy/app/models/.keep
@@ -509,6 +520,7 @@ test_files:
509
520
  - spec/dummy/app/models/employee.rb
510
521
  - spec/dummy/app/models/employee/collection.rb
511
522
  - spec/dummy/app/models/project.rb
523
+ - spec/dummy/app/models/project/collection.rb
512
524
  - spec/dummy/app/views/application/_form_errors.html.slim
513
525
  - spec/dummy/app/views/employees/_form.html.erb
514
526
  - spec/dummy/app/views/employees/collection_edit.html.slim
@@ -517,6 +529,12 @@ test_files:
517
529
  - spec/dummy/app/views/employees/new.html.erb
518
530
  - spec/dummy/app/views/employees/show.html.erb
519
531
  - spec/dummy/app/views/layouts/application.html.slim
532
+ - spec/dummy/app/views/projects/_form.html.erb
533
+ - spec/dummy/app/views/projects/collection_edit.html.slim
534
+ - spec/dummy/app/views/projects/edit.html.erb
535
+ - spec/dummy/app/views/projects/index.html.slim
536
+ - spec/dummy/app/views/projects/new.html.erb
537
+ - spec/dummy/app/views/projects/show.html.erb
520
538
  - spec/dummy/bin/bundle
521
539
  - spec/dummy/bin/rails
522
540
  - spec/dummy/bin/rake
@@ -551,7 +569,9 @@ test_files:
551
569
  - spec/dummy/public/422.html
552
570
  - spec/dummy/public/500.html
553
571
  - spec/dummy/public/favicon.ico
572
+ - spec/features/disabled_boolean_spec.rb
554
573
  - spec/features/multi_select_spec.rb
574
+ - spec/features/optional_boolean_with_normal_resource_spec.rb
555
575
  - spec/features/optional_text_field_with_normal_resource_spec.rb
556
576
  - spec/features/optionals_with_one_record_spec.rb
557
577
  - spec/features/translations_spec.rb
@@ -559,3 +579,4 @@ test_files:
559
579
  - spec/rails/form_builder_spec.rb
560
580
  - spec/record_selection/base_spec.rb
561
581
  - spec/spec_helper.rb
582
+ has_rdoc: