dryparams 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.md +44 -0
- data/Rakefile +22 -0
- data/lib/dryparams/dryable.rb +71 -0
- data/lib/dryparams/engine.rb +9 -0
- data/lib/dryparams/naming.rb +32 -0
- data/lib/dryparams/version.rb +18 -0
- data/lib/dryparams.rb +8 -0
- data/lib/tasks/dryparams_tasks.rake +4 -0
- data/test/dryparams/dryable_test.rb +34 -0
- data/test/dryparams/engine_test.rb +17 -0
- data/test/dryparams/naming_test.rb +38 -0
- data/test/dryparams/version_test.rb +30 -0
- data/test/dryparams_test.rb +7 -0
- data/test/dummy/README.md +27 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/javascripts/users.js +2 -0
- data/test/dummy/app/assets/stylesheets/application.css +13 -0
- data/test/dummy/app/assets/stylesheets/scaffold.css +56 -0
- data/test/dummy/app/assets/stylesheets/users.css +4 -0
- data/test/dummy/app/controllers/application_controller.rb +7 -0
- data/test/dummy/app/controllers/users_controller.rb +47 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/helpers/users_helper.rb +2 -0
- data/test/dummy/app/models/user.rb +2 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/app/views/users/_form.html.erb +29 -0
- data/test/dummy/app/views/users/edit.html.erb +6 -0
- data/test/dummy/app/views/users/index.html.erb +31 -0
- data/test/dummy/app/views/users/new.html.erb +5 -0
- data/test/dummy/app/views/users/show.html.erb +19 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/config/application.rb +23 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +29 -0
- data/test/dummy/config/environments/production.rb +80 -0
- data/test/dummy/config/environments/test.rb +36 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +5 -0
- data/test/dummy/config/initializers/secret_token.rb +12 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +58 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/db/migrate/20130815184453_create_users.rb +11 -0
- data/test/dummy/db/schema.rb +24 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/test.log +354 -0
- data/test/dummy/public/404.html +58 -0
- data/test/dummy/public/422.html +58 -0
- data/test/dummy/public/500.html +57 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/test/controllers/users_controller_test.rb +20 -0
- data/test/dummy/test/fixtures/users.yml +11 -0
- data/test/dummy/test/helpers/users_helper_test.rb +7 -0
- data/test/dummy/test/models/user_test.rb +7 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/371bf96e99717688ed7313a0c53f4212 +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/4050a4e5062ab95c9f32e9b6940821ea +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/5f1a0d05e77ca8b9a1fc2a47e17a8174 +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/6fc757c2c8329244ca95d6909865bbc2 +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/87b209c0c9da28094a8d5581a21262c6 +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/f56253b5f374fff1a33fbbc9881c9124 +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
- data/test/test_helper.rb +21 -0
- metadata +262 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2013 Geoffrey Roguelon
|
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/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# DRYParams [![Build Status](https://travis-ci.org/GRoguelon/DRYParams.png?branch=master)](https://travis-ci.org/GRoguelon/DRYParams) [![Coverage Status](https://coveralls.io/repos/GRoguelon/DRYParams/badge.png)](https://coveralls.io/r/GRoguelon/DRYParams) [![Code Climate](https://codeclimate.com/github/GRoguelon/DRYParams.png)](https://codeclimate.com/github/GRoguelon/DRYParams) [![Dependency Status](https://gemnasium.com/GRoguelon/DRYParams.png)](https://gemnasium.com/GRoguelon/DRYParams)
|
2
|
+
|
3
|
+
This gem is only compatible with Ruby on Rails 4.0 and the new feature _Strong Parameters_.
|
4
|
+
|
5
|
+
The DRYParams gem targets to DRY (Don't Repeat Yourself) the controller code in factoring the private mthods.
|
6
|
+
|
7
|
+
## Next
|
8
|
+
|
9
|
+
- Support Rails 3 with Strong Parameters gem.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'dryparams'
|
17
|
+
```
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
```bash
|
21
|
+
$ bundle
|
22
|
+
```
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
```bash
|
27
|
+
$ gem install dryparams
|
28
|
+
```
|
29
|
+
|
30
|
+
## Documentation
|
31
|
+
|
32
|
+
To get more informations about this gem, please visit [RubyDoc](http://rubydoc.info/gems/dryparams/frames/).
|
33
|
+
|
34
|
+
## Contributing
|
35
|
+
|
36
|
+
1. Fork it
|
37
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
38
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
39
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
40
|
+
5. Create new Pull Request
|
41
|
+
|
42
|
+
## License
|
43
|
+
|
44
|
+
MIT License. See the MIT-LICENSE file.
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'yard'
|
4
|
+
require 'rake/testtask'
|
5
|
+
rescue LoadError
|
6
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
7
|
+
end
|
8
|
+
|
9
|
+
Bundler::GemHelper.install_tasks
|
10
|
+
|
11
|
+
YARD::Rake::YardocTask.new do |t|
|
12
|
+
t.files = %w{lib/**/*.rb}
|
13
|
+
t.options = %w{-o doc --files README.md}
|
14
|
+
end
|
15
|
+
|
16
|
+
Rake::TestTask.new(:test) do |t|
|
17
|
+
t.libs << 'lib'
|
18
|
+
t.libs << 'test'
|
19
|
+
t.pattern = 'test/**/*_test.rb'
|
20
|
+
t.verbose = false
|
21
|
+
end
|
22
|
+
task default: :test
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'dryparams/naming'
|
2
|
+
|
3
|
+
module DRYParams
|
4
|
+
# The main class of the gem DRYParams.
|
5
|
+
#
|
6
|
+
# @author {mailto:geoffrey.roguelon@gmail.com Geoffrey Roguelon}
|
7
|
+
module Dryable
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
# The class methods of the +DRYParams+ concern.
|
11
|
+
#
|
12
|
+
# @author {mailto:geoffrey.roguelon@gmail.com Geoffrey Roguelon}
|
13
|
+
module ClassMethods
|
14
|
+
# The default actions for callback controller.
|
15
|
+
DEFAULT_ACTIONS = [:show, :edit, :update, :destroy]
|
16
|
+
|
17
|
+
# Add to the instance of the controller:
|
18
|
+
# - One method to set properly the instance variable from the model name;
|
19
|
+
# - One method to filter the parameters of the request.
|
20
|
+
#
|
21
|
+
# @param [Array] args the arguments of the generations methods.
|
22
|
+
# @return [void]
|
23
|
+
def set_object_and_filter_params(*args)
|
24
|
+
name, options = Naming.new(self), args.extract_options!
|
25
|
+
|
26
|
+
set_current_object(options, name)
|
27
|
+
filter_params(args, name)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Filter the parameters of the request.
|
31
|
+
#
|
32
|
+
# @param [Array] attributes the attributes to permit in the request.
|
33
|
+
# @param [Naming] name an instance of +Naming+ class.
|
34
|
+
# @return [void]
|
35
|
+
def filter_params(attributes, name = Naming.new(self))
|
36
|
+
if attributes.present?
|
37
|
+
self.class_eval(<<-RUBY, 'dryable.rb', 37)
|
38
|
+
def #{name.object}_params
|
39
|
+
params.require(:#{name.object}).permit(*#{attributes})
|
40
|
+
end
|
41
|
+
private :#{name.object}_params
|
42
|
+
RUBY
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Set properly the instance variable from the model name.
|
47
|
+
#
|
48
|
+
# @param [HashWithIndifferentAccess, Hash] options the option of the
|
49
|
+
# callback (:only, :except).
|
50
|
+
# @param [Naming] name an instance of +Naming+ class.
|
51
|
+
# @return [void]
|
52
|
+
def set_current_object(options, name = Naming.new(self))
|
53
|
+
if name.model
|
54
|
+
options = options.with_indifferent_access
|
55
|
+
methods = options.slice(:only, :except).presence || {
|
56
|
+
only: DEFAULT_ACTIONS
|
57
|
+
}
|
58
|
+
|
59
|
+
self.class_eval(<<-RUBY, 'dryable.rb', 59)
|
60
|
+
def set_#{name.object}
|
61
|
+
@#{name.object} = #{name.klass}.find(params[:id])
|
62
|
+
end
|
63
|
+
private :set_#{name.object}
|
64
|
+
|
65
|
+
before_action :set_#{name.object}, #{methods}
|
66
|
+
RUBY
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
module DRYParams
|
2
|
+
# The class define a callback to include the main module in
|
3
|
+
# +ActionController+.
|
4
|
+
#
|
5
|
+
# @author {mailto:geoffrey.roguelon@gmail.com Geoffrey Roguelon}
|
6
|
+
class Engine < ::Rails::Engine
|
7
|
+
ActiveSupport.on_load(:action_controller) { include DRYParams::Dryable }
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module DRYParams
|
2
|
+
# The class returns the different names available for a given controller.
|
3
|
+
#
|
4
|
+
# @author {mailto:geoffrey.roguelon@gmail.com Geoffrey Roguelon}
|
5
|
+
class Naming
|
6
|
+
# @param [ActionController::Base] controller_instance The controller used
|
7
|
+
# to determine the different names.
|
8
|
+
# @return [self]
|
9
|
+
def initialize(controller_instance)
|
10
|
+
@controller = controller_instance
|
11
|
+
end
|
12
|
+
|
13
|
+
# @return [Class] the model class associated to the controller.
|
14
|
+
def model
|
15
|
+
@model ||= begin
|
16
|
+
klass.constantize
|
17
|
+
rescue NameError
|
18
|
+
nil
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# @return [String] the object name from the controller.
|
23
|
+
def object
|
24
|
+
@object ||= klass.downcase
|
25
|
+
end
|
26
|
+
|
27
|
+
# @return [String] the model name associated to the controller.
|
28
|
+
def klass
|
29
|
+
@klass ||= @controller.controller_name.classify
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module DRYParams
|
2
|
+
# The module defines the version of the gem.
|
3
|
+
#
|
4
|
+
# @author {mailto:geoffrey.roguelon@gmail.com Geoffrey Roguelon}
|
5
|
+
module Version
|
6
|
+
# The major number version.
|
7
|
+
MAJOR = 0
|
8
|
+
# The minor number version.
|
9
|
+
MINOR = 1
|
10
|
+
# The tiny number version.
|
11
|
+
TINY = 0
|
12
|
+
# The pre realese number version.
|
13
|
+
PRE = nil
|
14
|
+
|
15
|
+
# The complete number version.
|
16
|
+
STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
|
17
|
+
end
|
18
|
+
end
|
data/lib/dryparams.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class DRYParams::DryableTest < ActiveSupport::TestCase
|
4
|
+
include DRYParams
|
5
|
+
|
6
|
+
test 'dryable should be a module' do
|
7
|
+
assert_kind_of Module, Dryable
|
8
|
+
end
|
9
|
+
|
10
|
+
test 'dryable should be extended by active_support::extended' do
|
11
|
+
assert Dryable.singleton_class.included_modules.include?(ActiveSupport::Concern)
|
12
|
+
end
|
13
|
+
|
14
|
+
test 'dryable should have a submodule class_methods' do
|
15
|
+
assert_kind_of Module, Dryable::ClassMethods
|
16
|
+
end
|
17
|
+
|
18
|
+
test 'class_methods should have a constant default_actions' do
|
19
|
+
assert Dryable::ClassMethods.const_defined?(:DEFAULT_ACTIONS)
|
20
|
+
assert_kind_of Array, Dryable::ClassMethods::DEFAULT_ACTIONS
|
21
|
+
end
|
22
|
+
|
23
|
+
test 'set_object_and_filter_params method should be defined' do
|
24
|
+
assert Dryable::ClassMethods.instance_methods.include?(:set_object_and_filter_params)
|
25
|
+
end
|
26
|
+
|
27
|
+
test 'filter_params method should be defined' do
|
28
|
+
assert Dryable::ClassMethods.instance_methods.include?(:filter_params)
|
29
|
+
end
|
30
|
+
|
31
|
+
test 'set_current_object method should be defined' do
|
32
|
+
assert Dryable::ClassMethods.instance_methods.include?(:set_current_object)
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class DRYParams::EngineTest < ActiveSupport::TestCase
|
4
|
+
include DRYParams
|
5
|
+
|
6
|
+
test 'engine should be a class' do
|
7
|
+
assert_kind_of Class, Engine
|
8
|
+
end
|
9
|
+
|
10
|
+
test 'engine should be herited from rails::engine' do
|
11
|
+
assert Engine < ::Rails::Engine
|
12
|
+
end
|
13
|
+
|
14
|
+
test 'dryable should be included in action_controller::base' do
|
15
|
+
ActionController::Base.ancestors.include?(DRYParams::Dryable)
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class DRYParams::NamingTest < ActiveSupport::TestCase
|
4
|
+
include DRYParams
|
5
|
+
|
6
|
+
class Gem < ActiveRecord::Base; end
|
7
|
+
class GemsController < ApplicationController; end
|
8
|
+
|
9
|
+
test 'naming should be a class' do
|
10
|
+
assert_kind_of Class, Naming
|
11
|
+
end
|
12
|
+
|
13
|
+
test 'DRYParams::Naming should have a constructor' do
|
14
|
+
assert_respond_to Naming, :new
|
15
|
+
end
|
16
|
+
|
17
|
+
test 'Constructor should take one argument' do
|
18
|
+
assert_instance_of Naming, Naming.new(GemsController)
|
19
|
+
end
|
20
|
+
|
21
|
+
test 'method model return a class' do
|
22
|
+
assert_equal Gem.name.split('::').last, Naming.new(GemsController).model.name
|
23
|
+
end
|
24
|
+
|
25
|
+
test 'method model return nil if class not found' do
|
26
|
+
assert_equal nil, Naming.new(DRYParams::Naming).model
|
27
|
+
end
|
28
|
+
|
29
|
+
test 'method object return string' do
|
30
|
+
assert_instance_of String, Naming.new(GemsController).object
|
31
|
+
assert_equal 'gem', Naming.new(GemsController).object
|
32
|
+
end
|
33
|
+
|
34
|
+
test 'method klass return string' do
|
35
|
+
assert_instance_of String, Naming.new(GemsController).klass
|
36
|
+
assert_equal 'Gem', Naming.new(GemsController).klass
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class DRYParams::VersionTest < ActiveSupport::TestCase
|
4
|
+
include DRYParams
|
5
|
+
|
6
|
+
test 'version should be a module' do
|
7
|
+
assert_kind_of Module, Version
|
8
|
+
end
|
9
|
+
|
10
|
+
test 'major constant must be defined' do
|
11
|
+
assert Version.const_defined?(:MAJOR)
|
12
|
+
end
|
13
|
+
|
14
|
+
test 'minor constant must be defined' do
|
15
|
+
assert Version.const_defined?(:MINOR)
|
16
|
+
end
|
17
|
+
|
18
|
+
test 'tiny constant must be defined' do
|
19
|
+
assert Version.const_defined?(:TINY)
|
20
|
+
end
|
21
|
+
|
22
|
+
test 'pre constant must be defined' do
|
23
|
+
assert Version.const_defined?(:PRE)
|
24
|
+
end
|
25
|
+
|
26
|
+
test 'string constant must be defined' do
|
27
|
+
assert Version.const_defined?(:STRING)
|
28
|
+
assert_instance_of String, Version::STRING
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,27 @@
|
|
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
|
+
Please feel free to use a different markup language if you do not plan to run
|
27
|
+
<tt>rake doc:app</tt>.
|
data/test/dummy/Rakefile
ADDED
@@ -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,47 @@
|
|
1
|
+
class UsersController < ApplicationController
|
2
|
+
set_object_and_filter_params :lastname, :firstname, :email
|
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
|
+
@user = User.new
|
16
|
+
end
|
17
|
+
|
18
|
+
# GET /users/1/edit
|
19
|
+
def edit
|
20
|
+
end
|
21
|
+
|
22
|
+
# POST /users
|
23
|
+
def create
|
24
|
+
@user = User.new(user_params)
|
25
|
+
|
26
|
+
if @user.save
|
27
|
+
redirect_to @user, notice: 'User was successfully created.'
|
28
|
+
else
|
29
|
+
render action: 'new'
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# PATCH/PUT /users/1
|
34
|
+
def update
|
35
|
+
if @user.update(user_params)
|
36
|
+
redirect_to @user, notice: 'User was successfully updated.'
|
37
|
+
else
|
38
|
+
render action: 'edit'
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# DELETE /users/1
|
43
|
+
def destroy
|
44
|
+
@user.destroy
|
45
|
+
redirect_to users_url, notice: 'User was successfully destroyed.'
|
46
|
+
end
|
47
|
+
end
|
@@ -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,29 @@
|
|
1
|
+
<%= form_for(@user) do |f| %>
|
2
|
+
<% if @user.errors.any? %>
|
3
|
+
<div id="error_explanation">
|
4
|
+
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
|
5
|
+
|
6
|
+
<ul>
|
7
|
+
<% @user.errors.full_messages.each do |msg| %>
|
8
|
+
<li><%= msg %></li>
|
9
|
+
<% end %>
|
10
|
+
</ul>
|
11
|
+
</div>
|
12
|
+
<% end %>
|
13
|
+
|
14
|
+
<div class="field">
|
15
|
+
<%= f.label :lastname %><br>
|
16
|
+
<%= f.text_field :lastname %>
|
17
|
+
</div>
|
18
|
+
<div class="field">
|
19
|
+
<%= f.label :firstname %><br>
|
20
|
+
<%= f.text_field :firstname %>
|
21
|
+
</div>
|
22
|
+
<div class="field">
|
23
|
+
<%= f.label :email %><br>
|
24
|
+
<%= f.text_field :email %>
|
25
|
+
</div>
|
26
|
+
<div class="actions">
|
27
|
+
<%= f.submit %>
|
28
|
+
</div>
|
29
|
+
<% end %>
|
@@ -0,0 +1,31 @@
|
|
1
|
+
<h1>Listing users</h1>
|
2
|
+
|
3
|
+
<table>
|
4
|
+
<thead>
|
5
|
+
<tr>
|
6
|
+
<th>Lastname</th>
|
7
|
+
<th>Firstname</th>
|
8
|
+
<th>Email</th>
|
9
|
+
<th></th>
|
10
|
+
<th></th>
|
11
|
+
<th></th>
|
12
|
+
</tr>
|
13
|
+
</thead>
|
14
|
+
|
15
|
+
<tbody>
|
16
|
+
<% @users.each do |user| %>
|
17
|
+
<tr>
|
18
|
+
<td><%= user.lastname %></td>
|
19
|
+
<td><%= user.firstname %></td>
|
20
|
+
<td><%= user.email %></td>
|
21
|
+
<td><%= link_to 'Show', user %></td>
|
22
|
+
<td><%= link_to 'Edit', edit_user_path(user) %></td>
|
23
|
+
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
|
24
|
+
</tr>
|
25
|
+
<% end %>
|
26
|
+
</tbody>
|
27
|
+
</table>
|
28
|
+
|
29
|
+
<br>
|
30
|
+
|
31
|
+
<%= link_to 'New User', new_user_path %>
|