Skip to content

Commit 0f80117

Browse files
author
Felipe Rodrigues Michetti
committed
Adding validations...
1 parent 036c714 commit 0f80117

File tree

4 files changed

+27
-26
lines changed

4 files changed

+27
-26
lines changed

api/app.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,14 @@ class App < Sinatra::Application
6060
}
6161

6262
helpers {
63-
ClassMap = create_classes(DATABASE, Dynamics)
64-
Classes = get_classes(Dynamics)
65-
66-
require 'extensions/models/dynamics_common'
67-
include Extensions::DynamicsCommon
68-
6963
def sample_method
7064
'sample method'
7165
end
7266
}
7367

68+
ClassMap = create_classes(DATABASE, Dynamics, false)
69+
Classes = get_classes(Dynamics)
70+
71+
require 'extensions/models/dynamics_common'
72+
include Extensions::DynamicsCommon
7473
end

lib/controllers/mini_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def included(controller)
5454
c.post('/:table_name') {|table_name|
5555
make_default_json_api(self, request.body.read, table_name) {|params, _status_code, _mapped_class, klass|
5656

57-
{status: 201, response: klass.create(params)&.values}
57+
{status: _status_code, response: klass.create(params)&.values}
5858
}
5959
}
6060

lib/helpers/api_helper.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def make_default_json_api(api_instance, payload = {}, table_name = nil)
2424
my_yield = table_name.nil? ? yield : yield(mapped_class, klass)
2525
block_given? ? response = my_yield : response = {msg: 'Api not implemented yet.'}
2626
rescue ModelException => e
27+
status = 400
2728
response = prepare_error_response(e)
2829
end
2930
[status, response.to_json.delete("\n")]
@@ -42,7 +43,8 @@ def make_default_json_api(api_instance, payload = {}, table_name = nil)
4243
response = {msg: 'Api not implemented yet.'}
4344
end
4445
rescue ModelException, ConstraintViolation, UniqueConstraintViolation, CheckConstraintViolation,
45-
NotNullConstraintViolation, ForeignKeyConstraintViolation, MassAssignmentRestriction => e
46+
NotNullConstraintViolation, ForeignKeyConstraintViolation, MassAssignmentRestriction, ValidationFailed => e
47+
status = 400
4648
response = prepare_error_response(e)
4749
end
4850
[status, response.to_json.delete("\n")]

lib/utils/class_factory.rb

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,22 @@ def create_classes(connection, module_constant, name_conflicts = true)
1313
table_info.each {|t|
1414
next if t[:table_name].eql?('configurations')
1515

16-
table_name = t[:table_name]
17-
table_schema = t[:schema]
18-
19-
table_constraints = table_constraints(connection, table_name)
20-
table_fks = table_foreign_keys(connection, table_name)
21-
t[:constraints] = table_constraints.map {|tc| {type: tc[:constraint_type], name: tc[:constraint_name]}}
22-
t[:foreign_keys] = table_fks
16+
name = t[:table_name]
17+
schema = t[:schema]
18+
19+
constraints = table_constraints(connection, name)
20+
foreign_keys = table_foreign_keys(connection, name)
21+
fields = scan_fields(connection, name)
22+
t[:constraints] = constraints.map {|tc| {type: tc[:constraint_type], name: tc[:constraint_name]}}
23+
t[:foreign_keys] = foreign_keys
2324
primary_key_name = t[:constraints].detect {|tc| tc[:type].eql?('PRIMARY KEY')}[:name] rescue nil
24-
25-
t[:columns_n_types] = table_columns(connection, table_name)
25+
t[:scanned_fields] = fields
26+
t[:columns_n_types] = table_columns(connection, name)
2627

2728
if name_conflicts
28-
fixed_name = underscore(table_name)
29+
fixed_name = underscore(name)
2930
else
30-
fixed_name = underscore(singularize(table_name))
31+
fixed_name = underscore(singularize(name))
3132
end
3233

3334
dynamic_name = pascalize(fixed_name)
@@ -36,12 +37,8 @@ def create_classes(connection, module_constant, name_conflicts = true)
3637

3738
begin
3839
Object.const_set(dynamic_name, Class.new(Sequel::Model) {|klass|
39-
set_dataset(connection[table_name.to_sym], inherited: false, require_valid_table: false)
40-
reset_fast_pk_lookup_sql
41-
42-
if table_fks
43-
44-
end
40+
set_dataset(connection[name.to_sym], inherited: false, require_valid_table: false)
41+
%i[validation_helpers column_conflicts dirty json_serializer auto_validations].each {|sym| plugin sym}
4542

4643
class << self
4744
define_method('find_by_id') {|id| self[id]}
@@ -90,7 +87,7 @@ def scan_classes(connection)
9087
end
9188

9289
def scan_fields(connection, table_name)
93-
query = SHOW_COLUMNS.gsub('?', table_name)
90+
query = SHOW_FIELDS.gsub('?', table_name)
9491
ConnectionFactory.execute_query(connection, query, false)
9592
end
9693

@@ -108,4 +105,7 @@ def scan_fields(connection, table_name)
108105

109106
# --EXIBINDO AS FOREIGN KEYS DAS TABELAS:
110107
SHOW_FOREIGN_KEYS = "SELECT tc.constraint_name , tc.table_name as source_table, kcu.column_name as source_column, ccu.table_name AS target_table, ccu.column_name AS target_column FROM information_schema.table_constraints AS tc JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name WHERE constraint_type = 'FOREIGN KEY' AND tc.table_name='?';".freeze
108+
109+
# -- QUERY PARA VALIDAÇÃO DE CAMPOS
110+
SHOW_FIELDS = "SELECT column_name, is_nullable, character_maximum_length, is_updatable FROM information_schema.columns WHERE table_name = '?';".freeze
111111
end

0 commit comments

Comments
 (0)