validation_sync 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +29 -0
  4. data/app/assets/javascripts/validation_sync.js +14 -0
  5. data/app/assets/javascripts/validations.js.coffee.erb +51 -0
  6. data/app/assets/stylesheets/validation_sync/application.css +13 -0
  7. data/app/helpers/validation_sync_form_helper.rb +87 -0
  8. data/config/routes.rb +0 -0
  9. data/lib/tasks/validation_sync_tasks.rake +4 -0
  10. data/lib/validation_sync.rb +4 -0
  11. data/lib/validation_sync/engine.rb +16 -0
  12. data/lib/validation_sync/version.rb +3 -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 +15 -0
  16. data/spec/dummy/app/assets/javascripts/posts.js +2 -0
  17. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  18. data/spec/dummy/app/assets/stylesheets/posts.css +4 -0
  19. data/spec/dummy/app/assets/stylesheets/scaffold.css +56 -0
  20. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  21. data/spec/dummy/app/controllers/posts_controller.rb +58 -0
  22. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  23. data/spec/dummy/app/helpers/posts_helper.rb +2 -0
  24. data/spec/dummy/app/models/post.rb +10 -0
  25. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  26. data/spec/dummy/app/views/posts/_form.html.erb +33 -0
  27. data/spec/dummy/app/views/posts/edit.html.erb +6 -0
  28. data/spec/dummy/app/views/posts/index.html.erb +33 -0
  29. data/spec/dummy/app/views/posts/new.html.erb +5 -0
  30. data/spec/dummy/app/views/posts/show.html.erb +24 -0
  31. data/spec/dummy/bin/bundle +3 -0
  32. data/spec/dummy/bin/rails +4 -0
  33. data/spec/dummy/bin/rake +4 -0
  34. data/spec/dummy/config.ru +4 -0
  35. data/spec/dummy/config/application.rb +30 -0
  36. data/spec/dummy/config/boot.rb +5 -0
  37. data/spec/dummy/config/database.yml +25 -0
  38. data/spec/dummy/config/environment.rb +5 -0
  39. data/spec/dummy/config/environments/development.rb +29 -0
  40. data/spec/dummy/config/environments/production.rb +80 -0
  41. data/spec/dummy/config/environments/test.rb +36 -0
  42. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  43. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  44. data/spec/dummy/config/initializers/inflections.rb +16 -0
  45. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  46. data/spec/dummy/config/initializers/secret_token.rb +12 -0
  47. data/spec/dummy/config/initializers/session_store.rb +3 -0
  48. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  49. data/spec/dummy/config/locales/en.yml +23 -0
  50. data/spec/dummy/config/routes.rb +3 -0
  51. data/spec/dummy/db/development.sqlite3 +0 -0
  52. data/spec/dummy/db/migrate/20140210065920_create_posts.rb +12 -0
  53. data/spec/dummy/db/schema.rb +25 -0
  54. data/spec/dummy/db/test.sqlite3 +0 -0
  55. data/spec/dummy/log/development.log +6345 -0
  56. data/spec/dummy/log/test.log +89 -0
  57. data/spec/dummy/public/404.html +58 -0
  58. data/spec/dummy/public/422.html +58 -0
  59. data/spec/dummy/public/500.html +57 -0
  60. data/spec/dummy/public/favicon.ico +0 -0
  61. data/spec/dummy/tmp/cache/assets/development/sprockets/04f730b2979d2f24cffafefd80e430f0 +0 -0
  62. data/spec/dummy/tmp/cache/assets/development/sprockets/0f11e0b3e0ece93d6c047318f2e5497d +0 -0
  63. data/spec/dummy/tmp/cache/assets/development/sprockets/0f53e3491015320ad54b7352dd6e9245 +0 -0
  64. data/spec/dummy/tmp/cache/assets/development/sprockets/10c6f05e183df4466be1e0171868fbd3 +0 -0
  65. data/spec/dummy/tmp/cache/assets/development/sprockets/122e92d06837af295d6c941999a8d48f +0 -0
  66. data/spec/dummy/tmp/cache/assets/development/sprockets/134039afc96b5a8d33c96d5da3816c1a +0 -0
  67. data/spec/dummy/tmp/cache/assets/development/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
  68. data/spec/dummy/tmp/cache/assets/development/sprockets/1e8f75a6b5b33970fb8e397347caa0a1 +0 -0
  69. data/spec/dummy/tmp/cache/assets/development/sprockets/24204f742af588c8166d3160b500d20f +0 -0
  70. data/spec/dummy/tmp/cache/assets/development/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
  71. data/spec/dummy/tmp/cache/assets/development/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
  72. data/spec/dummy/tmp/cache/assets/development/sprockets/371bf96e99717688ed7313a0c53f4212 +0 -0
  73. data/spec/dummy/tmp/cache/assets/development/sprockets/416150dc3ac35079c94273cc46e90aa6 +0 -0
  74. data/spec/dummy/tmp/cache/assets/development/sprockets/43b02dba04222f7470018fdda830925e +0 -0
  75. data/spec/dummy/tmp/cache/assets/development/sprockets/467cb2a85cb807b3c28bacd8f6147dfc +0 -0
  76. data/spec/dummy/tmp/cache/assets/development/sprockets/510da110ae528e2d22533be39ff696c5 +0 -0
  77. data/spec/dummy/tmp/cache/assets/development/sprockets/5384ad85f52d3272dbc64d46ef3876a4 +0 -0
  78. data/spec/dummy/tmp/cache/assets/development/sprockets/56146675dbad13c72d3eb9ebd823dc11 +0 -0
  79. data/spec/dummy/tmp/cache/assets/development/sprockets/5957a00e12da95bda275e8c3565a0316 +0 -0
  80. data/spec/dummy/tmp/cache/assets/development/sprockets/62d3fdf824fa141115db660a17ab94bc +0 -0
  81. data/spec/dummy/tmp/cache/assets/development/sprockets/6fc757c2c8329244ca95d6909865bbc2 +0 -0
  82. data/spec/dummy/tmp/cache/assets/development/sprockets/76cec3171960b0a527249d215835fade +0 -0
  83. data/spec/dummy/tmp/cache/assets/development/sprockets/87283d82c809770d9da81fe4e72a2abe +0 -0
  84. data/spec/dummy/tmp/cache/assets/development/sprockets/97892dcc76bfd97e09ab26d3f7a6d522 +0 -0
  85. data/spec/dummy/tmp/cache/assets/development/sprockets/c71eea08c37d8da406449ab45486b070 +0 -0
  86. data/spec/dummy/tmp/cache/assets/development/sprockets/c85016e7bbd4f3adbb7635d01f85d39b +0 -0
  87. data/spec/dummy/tmp/cache/assets/development/sprockets/cbc66bd36b3aa4411f9ce20da0fd37a0 +0 -0
  88. data/spec/dummy/tmp/cache/assets/development/sprockets/cc1ff523f6e3d417ee0a9bbbfe23a69b +0 -0
  89. data/spec/dummy/tmp/cache/assets/development/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
  90. data/spec/dummy/tmp/cache/assets/development/sprockets/d066c004d1fd26ae76a61303a7a18145 +0 -0
  91. data/spec/dummy/tmp/cache/assets/development/sprockets/d5a2d5bfe451248efe5aa31d1c94575c +0 -0
  92. data/spec/dummy/tmp/cache/assets/development/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
  93. data/spec/dummy/tmp/cache/assets/development/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
  94. data/spec/dummy/tmp/cache/assets/test/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
  95. data/spec/dummy/tmp/cache/assets/test/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
  96. data/spec/dummy/tmp/cache/assets/test/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
  97. data/spec/dummy/tmp/cache/assets/test/sprockets/371bf96e99717688ed7313a0c53f4212 +0 -0
  98. data/spec/dummy/tmp/cache/assets/test/sprockets/416150dc3ac35079c94273cc46e90aa6 +0 -0
  99. data/spec/dummy/tmp/cache/assets/test/sprockets/5384ad85f52d3272dbc64d46ef3876a4 +0 -0
  100. data/spec/dummy/tmp/cache/assets/test/sprockets/6fc757c2c8329244ca95d6909865bbc2 +0 -0
  101. data/spec/dummy/tmp/cache/assets/test/sprockets/c85016e7bbd4f3adbb7635d01f85d39b +0 -0
  102. data/spec/dummy/tmp/cache/assets/test/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
  103. data/spec/dummy/tmp/cache/assets/test/sprockets/d066c004d1fd26ae76a61303a7a18145 +0 -0
  104. data/spec/dummy/tmp/cache/assets/test/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
  105. data/spec/dummy/tmp/cache/assets/test/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
  106. data/spec/dummy/tmp/pids/server.pid +1 -0
  107. data/spec/features/form_validation_spec.rb +53 -0
  108. data/spec/helpers/form_helper_spec.rb +4 -0
  109. data/spec/spec_helper.rb +26 -0
  110. metadata +388 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d76e0ebef4820b514deda0bf1dfa67bb968116a5
4
+ data.tar.gz: 6e6df3a6b7cd6fd6c8cb64943eccdde8824ad552
5
+ SHA512:
6
+ metadata.gz: 73f2e45538de52e687d1b060cb30e3705fa5152e7c3c1d159fc98aea9054d00ca577527e83c1e20cceafc9bd246d96aeff4966c01ae1ae5d85c1f8a571acdae3
7
+ data.tar.gz: c203522a5801e91c2e06b5afc88c337cfaccaa89c7080be6ed9aa64472f132fc52d401b90d217022395ea1c7af7acbcc68876623976d10d135b009255b210788
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 Nathan Hackley
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.
data/Rakefile ADDED
@@ -0,0 +1,29 @@
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 = 'ValidationSync'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each {|f| load f }
25
+ require 'rspec/core'
26
+ require 'rspec/core/rake_task'
27
+ desc "Run all specs in spec directory (excluding plugin specs)"
28
+ RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')
29
+ task :default => :spec
@@ -0,0 +1,14 @@
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 ../../../vendor/assets/javascripts/.
14
+ //= require_tree .
@@ -0,0 +1,51 @@
1
+ # bring in ValidationSyncFormHelper module
2
+ <% environment.context_class.instance_eval { include ValidationSyncFormHelper } %>
3
+
4
+ ValidationSync =
5
+
6
+ init: ->
7
+ <% find_all_models.each do |model| %>
8
+ # validate if a relevant form is present
9
+ ValidationSync.validate<%= model[:name].camelize %>() if $(".new_<%= model[:name] %>[data-sync-validations='true'], .edit_<%= model[:name] %>[data-sync-validations='true']").length
10
+ <% end %>
11
+
12
+
13
+
14
+ # build a validateModel() method for each ActiveRecord model
15
+ <% find_all_models.each do |model| %>
16
+
17
+ validate<%= model[:name].camelize %>: ->
18
+
19
+ # trigger validation
20
+ $(".new_<%= model[:name] %>[data-sync-validations='true'], .edit_<%= model[:name] %>[data-sync-validations='true']").validate
21
+
22
+ # what attributes should we validate?
23
+ rules:
24
+
25
+ <% model[:attributes].each do |attr| %>
26
+ "<%= model[:name] %>[<%= attr %>]":
27
+ required: <%= check_model_validation(model[:name], attr, ActiveRecord::Validations::PresenceValidator) %>
28
+ number: <%= check_model_validation(model[:name], attr, ActiveModel::Validations::NumericalityValidator) %>
29
+ minlength: '<%= get_validation_attr(model[:name], attr, ActiveModel::Validations::LengthValidator, :minimum) %>'
30
+
31
+ <% unless get_validation_attr(model[:name], attr, ActiveModel::Validations::LengthValidator, :maximum).nil? %>
32
+ maxlength: '<%= get_validation_attr(model[:name], attr, ActiveModel::Validations::LengthValidator, :maximum) %>'
33
+ <% end %>
34
+ <% end %>
35
+
36
+
37
+ # what error messages should show?
38
+ messages:
39
+
40
+ <% model[:attributes].each do |attr| %>
41
+ "<%= model[:name] %>[<%= attr %>]":
42
+ required: "<%= get_validation_message(model[:name], attr, ActiveRecord::Validations::PresenceValidator) %>"
43
+ number: "<%= get_validation_message(model[:name], attr, ActiveModel::Validations::NumericalityValidator) %>"
44
+ <% end %>
45
+
46
+ <% end # find_all_models.each %>
47
+
48
+
49
+
50
+ # Fire init() on ready (page:load for turbolinks)
51
+ $(document).on('ready page:load', ValidationSync.init)
@@ -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,87 @@
1
+ module ValidationSyncFormHelper
2
+
3
+ def form_for(record, options = {}, &block)
4
+ # If the html_options / data hashes are not
5
+ # set, then let's set them as an empty hash
6
+ options[:html] ||= {}
7
+ options[:html][:data] ||= {}
8
+
9
+ # Add our data-sync-validations attribute
10
+ options[:html][:data][:sync_validations] = options[:validate].to_s
11
+
12
+ super
13
+ end
14
+
15
+
16
+ def find_all_models
17
+ models = []
18
+
19
+ # iterate over all files in folder
20
+ folder = File.join(ValidationSync::Engine::APP_ROOT, "app", "models")
21
+
22
+ Dir[File.join(folder, "*")].each do |filename|
23
+ klass = File.basename(filename, '.rb').camelize.constantize
24
+ next unless klass.ancestors.include?(ActiveRecord::Base)
25
+
26
+ model = {
27
+ name: klass.name.downcase,
28
+ attributes: klass.new.attributes.keys
29
+ }
30
+
31
+ models << model
32
+ end
33
+
34
+ return models
35
+ end
36
+
37
+
38
+ #
39
+ # Check to see if a given model attribute uses a given validation
40
+ # returns true/false
41
+ #
42
+ def check_model_validation(object_name, method, validation)
43
+ object_class = object_name.camelize.constantize
44
+ if object_class.validators_on(method).map(&:class).include? validation
45
+ true
46
+ else
47
+ false
48
+ end
49
+ end
50
+
51
+
52
+ #
53
+ # Check to see if a given model attribute uses a given validation
54
+ # returns the specified validation option
55
+ #
56
+ def get_validation_attr(object_name, method, validation, option)
57
+ if check_model_validation(object_name, method, validation)
58
+ object_class = object_name.camelize.constantize
59
+ attr = object_class.validators_on(method).find { |v| v.class == validation }
60
+ return (attr.options[option]) ? attr.options[option] : nil
61
+ end
62
+ end
63
+
64
+
65
+ def get_validation_message(object_name, method, validation)
66
+ object_class = object_name.camelize.constantize
67
+
68
+ # we validate this attribute
69
+ if val = object_class.validators_on(method).find { |v| v.class == validation }
70
+
71
+ if val.options[:message]
72
+ # custom message is defined
73
+ return val.options[:message]
74
+ else
75
+ # fall back to the default message
76
+ return "TODO: add default error messages"
77
+ end
78
+
79
+ else
80
+ # we don't validate this attribute
81
+ nil
82
+ end
83
+ end
84
+
85
+
86
+ end
87
+
data/config/routes.rb ADDED
File without changes
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :validation_sync do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,4 @@
1
+ require "validation_sync/engine"
2
+
3
+ module ValidationSync
4
+ end
@@ -0,0 +1,16 @@
1
+ module ValidationSync
2
+ class Engine < ::Rails::Engine
3
+ # isolate_namespace ValidationSync
4
+
5
+ config.generators do |g|
6
+ g.test_framework :rspec, :fixture => false
7
+ g.fixture_replacement :factory_girl, :dir => 'spec/factories'
8
+ g.assets false
9
+ g.helper false
10
+ end
11
+
12
+ initializer "validation_sync.load_app_instance_data" do |app|
13
+ APP_ROOT = app.root
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module ValidationSync
2
+ VERSION = "0.0.2"
3
+ end
@@ -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,15 @@
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 jquery
14
+ //= require validation_sync
15
+ //= require_tree .
@@ -0,0 +1,2 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.
@@ -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,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -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,58 @@
1
+ class PostsController < ApplicationController
2
+ before_action :set_post, only: [:show, :edit, :update, :destroy]
3
+
4
+ # GET /posts
5
+ def index
6
+ @posts = Post.all
7
+ end
8
+
9
+ # GET /posts/1
10
+ def show
11
+ end
12
+
13
+ # GET /posts/new
14
+ def new
15
+ @post = Post.new
16
+ end
17
+
18
+ # GET /posts/1/edit
19
+ def edit
20
+ end
21
+
22
+ # POST /posts
23
+ def create
24
+ @post = Post.new(post_params)
25
+
26
+ if @post.save
27
+ redirect_to @post, notice: 'Post was successfully created.'
28
+ else
29
+ render action: 'new'
30
+ end
31
+ end
32
+
33
+ # PATCH/PUT /posts/1
34
+ def update
35
+ if @post.update(post_params)
36
+ redirect_to @post, notice: 'Post was successfully updated.'
37
+ else
38
+ render action: 'edit'
39
+ end
40
+ end
41
+
42
+ # DELETE /posts/1
43
+ def destroy
44
+ @post.destroy
45
+ redirect_to posts_url, notice: 'Post was successfully destroyed.'
46
+ end
47
+
48
+ private
49
+ # Use callbacks to share common setup or constraints between actions.
50
+ def set_post
51
+ @post = Post.find(params[:id])
52
+ end
53
+
54
+ # Only allow a trusted parameter "white list" through.
55
+ def post_params
56
+ params.require(:post).permit(:title, :content, :view_count, :author_email)
57
+ end
58
+ end