forest_rails 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +1 -0
  4. data/Rakefile +37 -0
  5. data/app/assets/javascripts/forest_rails/application.js +13 -0
  6. data/app/assets/stylesheets/forest_rails/application.css +15 -0
  7. data/app/assets/stylesheets/scaffold.css +56 -0
  8. data/app/controllers/forest_rails/apimaps_controller.rb +19 -0
  9. data/app/controllers/forest_rails/application_controller.rb +13 -0
  10. data/app/controllers/forest_rails/resources_controller.rb +48 -0
  11. data/app/deserializers/forest_rails/resource_deserializer.rb +34 -0
  12. data/app/helpers/forest_rails/application_helper.rb +4 -0
  13. data/app/models/forest_rails/collection.rb +26 -0
  14. data/app/serializers/forest_rails/apimap_serializer.rb +5 -0
  15. data/app/serializers/forest_rails/serializer_factory.rb +45 -0
  16. data/app/services/forest_rails/schema_adapter.rb +84 -0
  17. data/app/views/layouts/forest_rails/application.html.erb +14 -0
  18. data/config/routes.rb +9 -0
  19. data/lib/forest_rails.rb +5 -0
  20. data/lib/forest_rails/engine.rb +5 -0
  21. data/lib/forest_rails/version.rb +3 -0
  22. data/lib/generators/forest_rails/install_generator.rb +15 -0
  23. data/lib/tasks/forest_rails_tasks.rake +4 -0
  24. data/test/deserializers/forest_rails/resource_deserializer_test.rb +38 -0
  25. data/test/dummy/README.rdoc +28 -0
  26. data/test/dummy/Rakefile +6 -0
  27. data/test/dummy/app/assets/javascripts/application.js +13 -0
  28. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  29. data/test/dummy/app/controllers/application_controller.rb +5 -0
  30. data/test/dummy/app/helpers/application_helper.rb +2 -0
  31. data/test/dummy/app/models/belongs_to_class_name_field.rb +3 -0
  32. data/test/dummy/app/models/belongs_to_field.rb +3 -0
  33. data/test/dummy/app/models/boolean_field.rb +2 -0
  34. data/test/dummy/app/models/date_field.rb +2 -0
  35. data/test/dummy/app/models/decimal_field.rb +2 -0
  36. data/test/dummy/app/models/float_field.rb +2 -0
  37. data/test/dummy/app/models/has_many_field.rb +3 -0
  38. data/test/dummy/app/models/has_one_field.rb +3 -0
  39. data/test/dummy/app/models/integer_field.rb +2 -0
  40. data/test/dummy/app/models/string_field.rb +2 -0
  41. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  42. data/test/dummy/bin/bundle +3 -0
  43. data/test/dummy/bin/rails +4 -0
  44. data/test/dummy/bin/rake +4 -0
  45. data/test/dummy/bin/setup +29 -0
  46. data/test/dummy/config.ru +4 -0
  47. data/test/dummy/config/application.rb +26 -0
  48. data/test/dummy/config/boot.rb +5 -0
  49. data/test/dummy/config/database.yml +25 -0
  50. data/test/dummy/config/environment.rb +5 -0
  51. data/test/dummy/config/environments/development.rb +41 -0
  52. data/test/dummy/config/environments/production.rb +79 -0
  53. data/test/dummy/config/environments/test.rb +42 -0
  54. data/test/dummy/config/initializers/assets.rb +11 -0
  55. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  56. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  57. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  58. data/test/dummy/config/initializers/inflections.rb +16 -0
  59. data/test/dummy/config/initializers/mime_types.rb +4 -0
  60. data/test/dummy/config/initializers/session_store.rb +3 -0
  61. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  62. data/test/dummy/config/locales/en.yml +23 -0
  63. data/test/dummy/config/routes.rb +4 -0
  64. data/test/dummy/config/secrets.yml +22 -0
  65. data/test/dummy/db/development.sqlite3 +0 -0
  66. data/test/dummy/db/migrate/20150608130516_create_date_field.rb +7 -0
  67. data/test/dummy/db/migrate/20150608131430_create_integer_field.rb +7 -0
  68. data/test/dummy/db/migrate/20150608131603_create_decimal_field.rb +7 -0
  69. data/test/dummy/db/migrate/20150608131610_create_float_field.rb +7 -0
  70. data/test/dummy/db/migrate/20150608132159_create_boolean_field.rb +7 -0
  71. data/test/dummy/db/migrate/20150608132621_create_string_field.rb +7 -0
  72. data/test/dummy/db/migrate/20150608133038_create_belongs_to_field.rb +7 -0
  73. data/test/dummy/db/migrate/20150608133044_create_has_one_field.rb +6 -0
  74. data/test/dummy/db/migrate/20150608150016_create_has_many_field.rb +6 -0
  75. data/test/dummy/db/migrate/20150609114636_create_belongs_to_class_name_field.rb +7 -0
  76. data/test/dummy/db/schema.rb +58 -0
  77. data/test/dummy/db/test.sqlite3 +0 -0
  78. data/test/dummy/log/development.log +91 -0
  79. data/test/dummy/log/test.log +4009 -0
  80. data/test/dummy/public/404.html +67 -0
  81. data/test/dummy/public/422.html +67 -0
  82. data/test/dummy/public/500.html +66 -0
  83. data/test/dummy/public/favicon.ico +0 -0
  84. data/test/forest_rails_test.rb +7 -0
  85. data/test/integration/navigation_test.rb +10 -0
  86. data/test/services/forest_rails/schema_adapter_test.rb +75 -0
  87. data/test/test_helper.rb +20 -0
  88. metadata +237 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: eb90ff5eb1c888204baadffd14d29914ce41deb7
4
+ data.tar.gz: dda697ca8577389ce88c382d09f275de1eecef3d
5
+ SHA512:
6
+ metadata.gz: bb0c27072b8cbd4961f68f0ca97e6414fa70114556dd4b539782dad10c9875c045eccd06f5e90b56df8a644ee547d83f415630a1c7653e2f0fc3bcad6d6ecd8d
7
+ data.tar.gz: 3d7ba83da65ea40fd160e90b8a9349d2aca89a43bd57ad11c9a43857173bea34da256b9da302bcaeb92931e469c3b7a251a862ebf7dff860fdd79ec20fc6bf70
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Sandro Munda
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.
@@ -0,0 +1 @@
1
+ = Forest Rails Liana
@@ -0,0 +1,37 @@
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 = 'ForestRails'
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("../test/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+ load 'rails/tasks/statistics.rake'
22
+
23
+
24
+
25
+ Bundler::GemHelper.install_tasks
26
+
27
+ require 'rake/testtask'
28
+
29
+ Rake::TestTask.new(:test) do |t|
30
+ t.libs << 'lib'
31
+ t.libs << 'test'
32
+ t.pattern = 'test/**/*_test.rb'
33
+ t.verbose = false
34
+ end
35
+
36
+
37
+ task default: :test
@@ -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 any plugin's vendor/assets/javascripts directory 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/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
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 any plugin's vendor/assets/stylesheets directory 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 bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -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,19 @@
1
+ module ForestRails
2
+ class ApimapsController < ForestRails::ApplicationController
3
+ def index
4
+ result = []
5
+
6
+ ActiveRecord::Base.connection.tables.map do |model_name|
7
+ begin
8
+ model = model_name.classify.constantize
9
+ result << SchemaAdapter.new(model).perform
10
+ rescue => error
11
+ puts error.inspect
12
+ end
13
+ end
14
+
15
+ render json: result, each_serializer: ApimapSerializer,
16
+ adapter: :json_api
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,13 @@
1
+ require 'jwt'
2
+
3
+ module ForestRails
4
+ class ApplicationController < ActionController::Base
5
+ before_filter :authenticate_user_from_jwt
6
+
7
+ def authenticate_user_from_jwt
8
+ JWT.decode request.headers[:Authorization].split[1],
9
+ ForestRails.jwt_signing_key
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,48 @@
1
+ module ForestRails
2
+ class ResourcesController < ForestRails::ApplicationController
3
+
4
+ before_filter :find_resource
5
+ before_filter :define_serializers
6
+
7
+ def index
8
+ render json: @resource.limit(20), each_serializer: @serializer,
9
+ adapter: :json_api
10
+ end
11
+
12
+ def show
13
+ render json: @resource.find(params[:id]), serializer: @serializer,
14
+ adapter: :json_api
15
+ end
16
+
17
+ def create
18
+ record = @resource.create!(resource_params)
19
+ render json: record, serializer: @serializer, adapter: :json_api
20
+ end
21
+
22
+ private
23
+
24
+ def find_resource
25
+ @resource_plural_name = params[:resource]
26
+ @resource_singular_name = @resource_plural_name.singularize
27
+ @resource_class_name = @resource_singular_name.classify
28
+
29
+ begin
30
+ @resource = @resource_class_name.constantize
31
+ rescue
32
+ end
33
+
34
+ if @resource.nil? || !@resource.ancestors.include?(ActiveRecord::Base)
35
+ render json: {status: 404}, status: :not_found
36
+ end
37
+ end
38
+
39
+ def define_serializers
40
+ @serializer = SerializerFactory.new.serializer_for(@resource)
41
+ end
42
+
43
+ def resource_params
44
+ ResourceDeserializer.new(params).perform
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,34 @@
1
+ module ForestRails
2
+ class ResourceDeserializer
3
+
4
+ def initialize(params)
5
+ @params = params
6
+ end
7
+
8
+ def perform
9
+ @attributes = extract_attributes
10
+ extract_relationships
11
+
12
+ @attributes
13
+ end
14
+
15
+ def extract_attributes
16
+ @params.require(:attributes).permit!
17
+ end
18
+
19
+ def extract_relationships
20
+ if @params[:relationships]
21
+ @params[:relationships].each do |name, relationship|
22
+ data = relationship[:data]
23
+
24
+ if data.is_a?(Hash)
25
+ @attributes[data[:type].singularize.foreign_key] = data[:id]
26
+ end
27
+
28
+ end
29
+ end
30
+
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,4 @@
1
+ module ForestRails
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,26 @@
1
+ module ForestRails
2
+ class Collection
3
+
4
+ include ActiveModel::Validations
5
+ include ActiveModel::Conversion
6
+ include ActiveModel::Serialization
7
+ extend ActiveModel::Naming
8
+
9
+ attr_accessor :name, :fields
10
+
11
+ def initialize(attributes = {})
12
+ attributes.each do |name, value|
13
+ send("#{name}=", value)
14
+ end
15
+ end
16
+
17
+ def persisted?
18
+ false
19
+ end
20
+
21
+ def id
22
+ name
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,5 @@
1
+ module ForestRails
2
+ class ApimapSerializer < ActiveModel::Serializer
3
+ attributes :name, :fields
4
+ end
5
+ end
@@ -0,0 +1,45 @@
1
+ module ForestRails
2
+ class SerializerFactory
3
+ def initialize
4
+ @serializers = {}
5
+ end
6
+
7
+ def serializer_for(active_record_class)
8
+ overriden_serializer(active_record_class) ||
9
+ registered_serializer(active_record_class) ||
10
+ generated_serializer(active_record_class)
11
+ end
12
+
13
+ private
14
+
15
+ def overriden_serializer(active_record_class)
16
+ begin
17
+ "ForestRails::#{active_record_class}Serializer".constantize;
18
+ rescue
19
+ end
20
+ end
21
+
22
+ def registered_serializer(active_record_class)
23
+ @serializers[key(active_record_class)]
24
+ end
25
+
26
+ def generated_serializer(active_record_class)
27
+ associations = active_record_class.reflect_on_all_associations
28
+ column_names = active_record_class.column_names
29
+ key = key(active_record_class)
30
+
31
+ serializer = @serializers[key] = Class.new(ActiveModel::Serializer)
32
+ serializer.attributes(*column_names)
33
+ associations.each do |association|
34
+ serializer.send(
35
+ association.macro, association.name,
36
+ serializer: serializer_for(association.active_record))
37
+ end
38
+ serializer
39
+ end
40
+
41
+ def key(active_record_class)
42
+ active_record_class.to_s.tableize.to_sym
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,84 @@
1
+ module ForestRails
2
+ class SchemaAdapter
3
+
4
+ def initialize(model)
5
+ @model = model
6
+ end
7
+
8
+ def perform
9
+ @collection = Collection.new({ name: @model.name.tableize, fields: [] })
10
+ add_columns
11
+ add_associations
12
+
13
+ @collection
14
+ end
15
+
16
+ private
17
+
18
+ def add_columns
19
+ @model.columns.each do |column|
20
+ @collection.fields << get_schema_for_column(column)
21
+ end
22
+ end
23
+
24
+ def add_associations
25
+ @model.reflect_on_all_associations.each do |association|
26
+ if schema = column_association(@collection, association)
27
+ schema[:ref] = get_ref_for(association)
28
+ else
29
+ @collection.fields << get_schema_for_association(association)
30
+ end
31
+ end
32
+ end
33
+
34
+ def get_schema_for_column(column)
35
+ { field: column.name, type: get_type_for(column) }
36
+ end
37
+
38
+ def get_schema_for_association(association)
39
+ {
40
+ field: association.name.to_s,
41
+ type: get_type_for_association(association),
42
+ ref: "#{association.name.to_s.tableize}.id"
43
+ }
44
+ end
45
+
46
+ def get_type_for(column)
47
+ case column.type
48
+ when :integer
49
+ 'Number'
50
+ when :float
51
+ 'Number'
52
+ when :decimal
53
+ 'Number'
54
+ when :datetime
55
+ 'Date'
56
+ when :date
57
+ 'Date'
58
+ when :string
59
+ 'String'
60
+ when :text
61
+ 'String'
62
+ when :boolean
63
+ 'Boolean'
64
+ end
65
+ end
66
+
67
+ def get_ref_for(association)
68
+ "#{association.class_name.to_s.tableize}.id"
69
+ end
70
+
71
+ def column_association(collection, field)
72
+ collection.fields.find {|x| x[:field] == field.foreign_key }
73
+ end
74
+
75
+ def get_type_for_association(association)
76
+ if association.macro == :has_many
77
+ '[Number]'
78
+ else
79
+ 'Number'
80
+ end
81
+ end
82
+
83
+ end
84
+ end