Skip to content

Commit 12f666c

Browse files
committed
rubocop -a
1 parent 37730c3 commit 12f666c

File tree

77 files changed

+648
-724
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+648
-724
lines changed

.rubocop.yml

+18
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,30 @@ Rails:
1717
Metrics/LineLength:
1818
Max: 150
1919

20+
Metrics/MethodLength:
21+
Max: 100
22+
23+
Metrics/BlockLength:
24+
Max: 50
25+
26+
Metrics/ClassLength:
27+
Enabled: false
28+
2029
Style/GuardClause:
2130
Enabled: false
2231

2332
Style/Documentation:
2433
Enabled: false
2534

35+
Style/ClassAndModuleChildren:
36+
Enabled: false
37+
38+
Naming/AccessorMethodName:
39+
Enabled: false
40+
41+
Naming/MemoizedInstanceVariableName:
42+
Enabled: false
43+
2644
# Prefer assert_not over assert !
2745
Rails/AssertNot:
2846
Include:

app/models/form_core/form.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ class Form < ApplicationRecord
66

77
self.table_name = "forms"
88

9-
has_many :fields
9+
has_many :fields, dependent: :destroy
1010
end
1111
end

db/migrate/20170430190404_create_forms.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
class CreateForms < ActiveRecord::Migration[5.1]
44
def change
55
create_table :forms do |t|
6-
t.string :name, null: false, index: {unique: true}
6+
t.string :name, null: false, index: { unique: true }
77
t.string :type, null: false, index: true
88

99
t.timestamps

lib/form_core.rb

+2-6
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ def virtual_model_class
1818
end
1919

2020
def virtual_model_class=(klass)
21-
unless klass && klass < VirtualModel
22-
raise ArgumentError, "#{klass} should be sub-class of #{VirtualModel}."
23-
end
21+
raise ArgumentError, "#{klass} should be sub-class of #{VirtualModel}." unless klass && klass < VirtualModel
2422

2523
@reserved_names = nil
2624
@virtual_model_class = klass
@@ -38,9 +36,7 @@ def virtual_model_coder_class
3836
end
3937

4038
def virtual_model_coder_class=(klass)
41-
unless klass && klass < Coder
42-
raise ArgumentError, "#{klass} should be sub-class of #{Coder}."
43-
end
39+
raise ArgumentError, "#{klass} should be sub-class of #{Coder}." unless klass && klass < Coder
4440

4541
@virtual_model_coder_class = klass
4642
end

lib/form_core/coder.rb

+12-12
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,20 @@ def load(_src)
2424

2525
private
2626

27-
def new_or_raise_decoding_error
28-
if strict?
29-
raise DecodingDataCorrupted
30-
else
31-
object_class.new
27+
def new_or_raise_decoding_error
28+
if strict?
29+
raise DecodingDataCorrupted
30+
else
31+
object_class.new
32+
end
3233
end
33-
end
3434

35-
def valid_attribute_names
36-
object_class.attribute_names + object_class._embeds_reflections.keys
37-
end
35+
def valid_attribute_names
36+
object_class.attribute_names + object_class._embeds_reflections.keys
37+
end
3838

39-
def valid_attributes(hash)
40-
hash.slice(*valid_attribute_names)
41-
end
39+
def valid_attributes(hash)
40+
hash.slice(*valid_attribute_names)
41+
end
4242
end
4343
end

lib/form_core/coders/hash_coder.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ def dump(obj)
99
end
1010

1111
def load(hash)
12-
if hash.nil? || !hash.respond_to?(:to_h)
13-
return new_or_raise_decoding_error
14-
end
12+
return new_or_raise_decoding_error if hash.nil? || !hash.respond_to?(:to_h)
1513

1614
object_class.new valid_attributes(hash)
1715
end

lib/form_core/coders/yaml_coder.rb

+3-7
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,15 @@ def dump(obj)
2323
def load(yaml)
2424
return object_class.new if yaml.blank?
2525

26-
unless yaml.is_a?(String) && /^---/.match?(yaml)
27-
return new_or_raise_decoding_error
28-
end
26+
return new_or_raise_decoding_error unless yaml.is_a?(String) && /^---/.match?(yaml)
2927

3028
decoded =
3129
if safe_mode?
3230
YAML.safe_load(yaml, YAMLCoder.whitelist_classes)
3331
else
34-
YAML.load(yaml)
32+
YAML.safe_load(yaml)
3533
end
36-
unless decoded.is_a? Hash
37-
return new_or_raise_decoding_error
38-
end
34+
return new_or_raise_decoding_error unless decoded.is_a? Hash
3935

4036
object_class.new valid_attributes(decoded)
4137
end

lib/form_core/concerns/models/field.rb

+13-19
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ module Field
88
NAME_REGEX = /\A[a-z][a-z_0-9]*\z/.freeze
99

1010
included do
11-
enum accessibility: {read_and_write: 0, readonly: 1, hidden: 2},
11+
enum accessibility: { read_and_write: 0, readonly: 1, hidden: 2 },
1212
_prefix: :access
1313

1414
serialize :validations
1515
serialize :options
1616

1717
validates :name,
1818
presence: true,
19-
uniqueness: {scope: :form},
20-
exclusion: {in: FormCore.reserved_names},
21-
format: {with: NAME_REGEX}
19+
uniqueness: { scope: :form },
20+
exclusion: { in: FormCore.reserved_names },
21+
format: { with: NAME_REGEX }
2222
validates :accessibility,
23-
inclusion: {in: accessibilities.keys.map(&:to_sym)}
23+
inclusion: { in: accessibilities.keys.map(&:to_sym) }
2424

2525
after_initialize do
2626
self.validations ||= {}
@@ -54,9 +54,7 @@ def interpret_to(model, overrides: {})
5454
default_value = overrides.fetch(:default_value, self.default_value)
5555
model.attribute name, stored_type, default: default_value
5656

57-
if accessibility == :readonly
58-
model.attr_readonly name
59-
end
57+
model.attr_readonly name if accessibility == :readonly
6058

6159
interpret_validations_to model, accessibility, overrides
6260
interpret_extra_to model, accessibility, overrides
@@ -66,22 +64,18 @@ def interpret_to(model, overrides: {})
6664

6765
protected
6866

69-
def interpret_validations_to(model, accessibility, overrides = {})
70-
validations = overrides.fetch(:validations, (self.validations || {}))
71-
validation_options = overrides.fetch(:validation_options) { self.options.fetch(:validation, {}) }
67+
def interpret_validations_to(model, accessibility, overrides = {})
68+
validations = overrides.fetch(:validations, (self.validations || {}))
69+
validation_options = overrides.fetch(:validation_options) { self.options.fetch(:validation, {}) }
7270

73-
if accessibility == :read_and_write && validations.present?
74-
model.validates name, **validations, **validation_options
71+
model.validates name, **validations, **validation_options if accessibility == :read_and_write && validations.present?
7572
end
76-
end
7773

78-
def interpret_extra_to(_model, _accessibility, _overrides = {}); end
74+
def interpret_extra_to(_model, _accessibility, _overrides = {}); end
7975

80-
def check_model_validity!(model)
81-
unless model.is_a?(Class) && model < ::FormCore::VirtualModel
82-
raise ArgumentError, "#{model} must be a #{::FormCore::VirtualModel}'s subclass"
76+
def check_model_validity!(model)
77+
raise ArgumentError, "#{model} must be a #{::FormCore::VirtualModel}'s subclass" unless model.is_a?(Class) && model < ::FormCore::VirtualModel
8378
end
84-
end
8579
end
8680
end
8781
end

lib/form_core/concerns/models/form.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ module Form
1111
validates :name,
1212
presence: true,
1313
uniqueness: true,
14-
exclusion: {in: FormCore.reserved_names},
15-
format: {with: NAME_REGEX}
14+
exclusion: { in: FormCore.reserved_names },
15+
format: { with: NAME_REGEX }
1616
end
1717

1818
def to_virtual_model(model_name: "Form",
@@ -38,11 +38,11 @@ def append_to_virtual_model(model,
3838

3939
private
4040

41-
def check_model_validity!(model)
42-
unless model.is_a?(Class) && model < ::FormCore::VirtualModel
43-
raise ArgumentError, "#{model} must be a #{::FormCore::VirtualModel}'s subclass"
41+
def check_model_validity!(model)
42+
unless model.is_a?(Class) && model < ::FormCore::VirtualModel
43+
raise ArgumentError, "#{model} must be a #{::FormCore::VirtualModel}'s subclass"
44+
end
4445
end
45-
end
4646
end
4747
end
4848
end

lib/form_core/virtual_model.rb

+2-6
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ def inspect
1111
inspection =
1212
if defined?(@attributes) && @attributes
1313
self.class.attribute_names.collect do |name|
14-
if has_attribute?(name)
15-
"#{name}: #{attribute_for_inspect(name)}"
16-
end
14+
"#{name}: #{attribute_for_inspect(name)}" if has_attribute?(name)
1715
end.compact.join(", ")
1816
else
1917
"not initialized"
@@ -51,9 +49,7 @@ def coder
5149
end
5250

5351
def coder=(klass)
54-
unless klass && klass < Coder
55-
raise ArgumentError, "#{klass} should be sub-class of #{Coder}."
56-
end
52+
raise ArgumentError, "#{klass} should be sub-class of #{Coder}." unless klass && klass < Coder
5753

5854
@_coder = klass.new(self)
5955
end

test/dummy/app/controllers/application_controller.rb

+11-11
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ class ApplicationController < ActionController::Base
77

88
private
99

10-
def set_time_zone(&block)
11-
Time.use_zone(current_time_zone, &block)
12-
end
10+
def set_time_zone(&block)
11+
Time.use_zone(current_time_zone, &block)
12+
end
1313

14-
def current_time_zone
15-
@_current_time_zone ||=
16-
if session[:current_time_zone].present?
17-
ActiveSupport::TimeZone[session[:current_time_zone]] || ActiveSupport::TimeZone["UTC"]
18-
else
19-
ActiveSupport::TimeZone["UTC"]
20-
end
21-
end
14+
def current_time_zone
15+
@_current_time_zone ||=
16+
if session[:current_time_zone].present?
17+
ActiveSupport::TimeZone[session[:current_time_zone]] || ActiveSupport::TimeZone["UTC"]
18+
else
19+
ActiveSupport::TimeZone["UTC"]
20+
end
21+
end
2222
end

test/dummy/app/controllers/dictionaries_controller.rb

+8-8
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ def destroy
4444

4545
private
4646

47-
# Use callbacks to share common setup or constraints between actions.
48-
def set_dictionary
49-
@dictionary = Dictionary.find(params[:id])
50-
end
47+
# Use callbacks to share common setup or constraints between actions.
48+
def set_dictionary
49+
@dictionary = Dictionary.find(params[:id])
50+
end
5151

52-
# Only allow a trusted parameter "white list" through.
53-
def dictionary_params
54-
params.fetch(:dictionary, {}).permit(:scope, :value)
55-
end
52+
# Only allow a trusted parameter "white list" through.
53+
def dictionary_params
54+
params.fetch(:dictionary, {}).permit(:scope, :value)
55+
end
5656
end

test/dummy/app/controllers/fields/application_controller.rb

+14-14
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@ class Fields::ApplicationController < ApplicationController
55

66
protected
77

8-
# Use callbacks to share common setup or constraints between actions.
9-
def set_field
10-
@field = Field.find(params[:field_id])
11-
end
8+
# Use callbacks to share common setup or constraints between actions.
9+
def set_field
10+
@field = Field.find(params[:field_id])
11+
end
1212

13-
def fields_url
14-
form = @field.form
13+
def fields_url
14+
form = @field.form
1515

16-
case form
17-
when Form
18-
form_fields_url(form)
19-
when NestedForm
20-
nested_form_fields_url(form)
21-
else
22-
raise "Unknown form: #{form.class}"
16+
case form
17+
when Form
18+
form_fields_url(form)
19+
when NestedForm
20+
nested_form_fields_url(form)
21+
else
22+
raise "Unknown form: #{form.class}"
23+
end
2324
end
24-
end
2525
end

test/dummy/app/controllers/fields/choices_controller.rb

+8-10
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,15 @@ def destroy
3434

3535
private
3636

37-
def require_attach_choices!
38-
unless @field.attached_choices?
39-
redirect_to fields_url
37+
def require_attach_choices!
38+
redirect_to fields_url unless @field.attached_choices?
4039
end
41-
end
4240

43-
def choice_params
44-
params.require(:choice).permit(:label)
45-
end
41+
def choice_params
42+
params.require(:choice).permit(:label)
43+
end
4644

47-
def set_choice
48-
@choice = @field.choices.find(params[:id])
49-
end
45+
def set_choice
46+
@choice = @field.choices.find(params[:id])
47+
end
5048
end

test/dummy/app/controllers/fields/data_source_options_controller.rb

+8-10
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,15 @@ def update
1717

1818
private
1919

20-
def require_data_source_options
21-
unless @field.respond_to?(:data_source)
22-
redirect_to fields_url
20+
def require_data_source_options
21+
redirect_to fields_url unless @field.respond_to?(:data_source)
2322
end
24-
end
2523

26-
def set_options
27-
@options = @field.data_source
28-
end
24+
def set_options
25+
@options = @field.data_source
26+
end
2927

30-
def options_params
31-
params.fetch(:options, {}).permit!
32-
end
28+
def options_params
29+
params.fetch(:options, {}).permit!
30+
end
3331
end

0 commit comments

Comments
 (0)