jetski 0.4.7 → 0.4.8

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 59cc948a27a477850ced800437233d2106f046a70eef581011efc407b29469ce
4
- data.tar.gz: 52dee7630e6efac75428fc46a5e18b3c2374323d0d77dd793cb77f3554f994fd
3
+ metadata.gz: 718959913722c396f5ada24d19c6997aee009d7a0aca229156a877def8dba9e4
4
+ data.tar.gz: 8ad9d4179950762af88e4f4df0a02ff4d8510050a5f7417af440b04f758a78dd
5
5
  SHA512:
6
- metadata.gz: 9f5780959202dac7113a038c9793863851147156fcda7d9ca0efa8b837afc8aaeb37f9f65a7ef266e4b3693dd2acfef48e1987f376e869d88640ec2fcad6c19e
7
- data.tar.gz: 76ca2114bc78e15d8fdac21a9f39b137cbf66fa0f3faf45948a245ffdf92722712a9099d55a285cbf0b83f886540b8c02fcc35730a08b91d9e418b2afefd2812
6
+ metadata.gz: c1c685e092ab8da1cf08805874d24f3e2319982f11c1a17ce14e92f0342b8ef5622a24cdd4db89e325c36e0a8c842e547057468b592d9028fdbff1fff84963bf
7
+ data.tar.gz: 847df89c32ce237683bd953da15d589236fe7884707e088e09d0512cc7554a3df09b05a493dd537586e11ac7e094ffc1142e443ea5649d5f1311aea329a669a4
@@ -8,7 +8,12 @@ module Jetski
8
8
  require_relative path_to_model
9
9
  # Call method to define model attributes after loading
10
10
  model_name = path_to_model.split("app/models/")[-1]
11
- .gsub(".rb", "").capitalize
11
+ .gsub(".rb", "")
12
+ .split("/")
13
+ .map(&:capitalize)
14
+ .join("::")
15
+ # posts/comment
16
+ # Post/comment
12
17
  model_class = Object.const_get(model_name)
13
18
  model_class.define_attribute_methods
14
19
  end
@@ -3,11 +3,11 @@ module Jetski
3
3
  class BaseController
4
4
  RESERVED_INSTANCE_VARIABLES = [
5
5
  :@res, :@performed_render, :@action_name,
6
- :@controller_name, :@controller_path, :@cookies, :@root,
7
- :@request_method, :@path
6
+ :@controller_name, :@controller_path, :@cookies
8
7
  ]
9
8
 
10
9
  include ReactiveForm
10
+ extend Jetski::Helpers::RouteHelpers
11
11
  attr_accessor :action_name, :controller_name, :controller_path,
12
12
  :params, :cookies
13
13
  attr_reader :res, :performed_render
@@ -52,17 +52,5 @@ module Jetski
52
52
  def get_cookie(name)
53
53
  cookies&.find { |c| c.name == name.to_s }&.value
54
54
  end
55
-
56
- def is_root?
57
- @root == true
58
- end
59
-
60
- def custom_path
61
- @path
62
- end
63
-
64
- def custom_request_method
65
- @request_method
66
- end
67
55
  end
68
56
  end
@@ -18,7 +18,6 @@ module Jetski
18
18
  _gen_sql += "create table #{pluralized_table_name} (\n"
19
19
 
20
20
  # Default fields on all models
21
-
22
21
  _gen_sql += " created_at datetime,\n"
23
22
  _gen_sql += " updated_at datetime,\n"
24
23
  _gen_sql += " id integer,\n"
@@ -0,0 +1,18 @@
1
+ # This is responsible for methods for routing in base_controller like the route class method
2
+
3
+ module Jetski
4
+ module Helpers
5
+ module RouteHelpers
6
+ def route(method_name, root: false, path: nil, request_method: nil)
7
+ # TODO: write code to set route
8
+ @custom_route_opts ||= {}
9
+ @custom_route_opts[method_name] = {
10
+ method_name: method_name,
11
+ root: root,
12
+ path: path,
13
+ request_method: request_method,
14
+ }
15
+ end
16
+ end
17
+ end
18
+ end
@@ -2,9 +2,50 @@ module Jetski
2
2
  module Helpers
3
3
  module ViewHelpers
4
4
  # Expecting a relative path.
5
- def render(partial_path)
6
- path_to_file = File.join(Jetski.app_root, 'app/views', path_to_controller, "_#{partial_path}.html.erb")
7
- File.read(path_to_file)
5
+ def render(partial_path, **locals)
6
+ path_to_file = File.join(Jetski.app_root, 'app/views',
7
+ path_to_controller, "_#{partial_path}.html.erb")
8
+ content = File.read(path_to_file)
9
+ bind = binding
10
+ locals.each do |k, v|
11
+ bind.local_variable_set k, v
12
+ end
13
+ template = ERB.new(content)
14
+ template.result(bind)
15
+ end
16
+
17
+ # link_to "New post", "/posts/new"
18
+ def link_to(link_text, url, **html_opts)
19
+ # TODO: Allow passing block to link_to
20
+ "<a href='#{url}' #{format_html_options(**html_opts)}>#{link_text}</a>"
21
+ end
22
+
23
+ # button_to("Click me", class: "cool-btn")
24
+ def button_to(button_text, **html_opts)
25
+ "<button #{format_html_options(**html_opts)}>#{button_text}</button>"
26
+ end
27
+
28
+ def input_tag(**html_opts)
29
+ "<input #{format_html_options(**html_opts)}>"
30
+ end
31
+
32
+ def textarea_tag(**html_opts)
33
+ "<textarea #{format_html_options(**html_opts.except(:value))}>#{html_opts[:value]}</textarea>"
34
+ end
35
+
36
+ def image_tag(image_path, **html_opts)
37
+ "<img src='#{image_path}' #{format_html_options(**html_opts)}></img>"
38
+ end
39
+
40
+ def favicon_tag(url, **html_opts)
41
+ "<link rel='icon' type='image/x-icon' #{format_html_options(**html_opts)} href='#{url}'>"
42
+ end
43
+
44
+ def format_html_options(**html_opts)
45
+ html_opts.map do |k, v|
46
+ formatted_key = k.to_s.gsub("_", "-")
47
+ "#{formatted_key}='#{v}'"
48
+ end.join(" ")
8
49
  end
9
50
  end
10
51
  end
data/lib/jetski/model.rb CHANGED
@@ -3,16 +3,13 @@ module Jetski
3
3
  extend Jetski::Database::Base
4
4
 
5
5
  def initialize(**args)
6
- # TODO: Need to fix code
7
- # Cannot redefine methods every time we initialize a new object.
8
- # need to define available methods on post when loading model
9
6
  @virtual_attributes = args
10
7
  end
11
8
 
12
9
  def inspect
13
10
  post_obj_id = object_id
14
- inspect_str = "#<Post:#{post_obj_id}"
15
- self.class.model_attributes.each do |attribute_name|
11
+ inspect_str = "#<#{self.class.to_s}:#{post_obj_id}"
12
+ self.class.attributes.each do |attribute_name|
16
13
  attribute_value = @virtual_attributes[attribute_name]
17
14
  inspect_str += " #{attribute_name}=\"#{attribute_value}\""
18
15
  end
@@ -66,7 +63,7 @@ module Jetski
66
63
  end
67
64
 
68
65
  def define_attribute_methods
69
- model_attributes.each do |attribute|
66
+ attributes.each do |attribute|
70
67
  define_method attribute do
71
68
  @virtual_attributes[attribute]
72
69
  end
@@ -91,6 +88,7 @@ module Jetski
91
88
  end
92
89
 
93
90
  def attributes
91
+ # TODO: Find a more performant way to get the column names from a table
94
92
  columns, *rows = db.execute2( "select * from #{pluralized_table_name}" )
95
93
  columns
96
94
  end
@@ -114,11 +112,6 @@ module Jetski
114
112
  table_name + "s"
115
113
  end
116
114
  end
117
-
118
- def model_attributes
119
- attributes.concat(["id", "created_at", "updated_at"])
120
- end
121
-
122
115
  private
123
116
  def format_model_obj(row, columns = nil)
124
117
  return unless row
@@ -24,18 +24,34 @@ module Jetski
24
24
  controller_name: controller_name,
25
25
  controller_path: controller_path,
26
26
  }
27
+ route_opts_hash = controller_class.instance_variable_get(:@custom_route_opts)
27
28
  action_names.each do |action_name|
28
29
  action_name = action_name.to_s
30
+ route_opts = route_opts_hash.fetch(action_name.to_sym, {})
31
+ custom_request_method = route_opts.fetch(:request_method, nil)
32
+ is_root = route_opts.fetch(:root, nil)
33
+ custom_path = route_opts.fetch(:path, nil)
34
+
35
+ url_to_use = proc do |url|
36
+ if is_root
37
+ "/"
38
+ elsif custom_path
39
+ custom_path
40
+ else
41
+ url
42
+ end
43
+ end
44
+
29
45
  case action_name
30
46
  when "new"
31
47
  auto_found_routes << base_opts.merge({
32
- url: controller_path + "/new",
48
+ url: url_to_use.call(controller_path + "/new"),
33
49
  method: "GET",
34
50
  action_name: action_name,
35
51
  })
36
52
  when "create"
37
53
  auto_found_routes << base_opts.merge({
38
- url: controller_path,
54
+ url: url_to_use.call(controller_path),
39
55
  method: "POST",
40
56
  action_name: action_name,
41
57
  })
@@ -53,21 +69,23 @@ module Jetski
53
69
  })
54
70
  when "update"
55
71
  auto_found_routes << base_opts.merge({
56
- url: controller_path + "/:id",
72
+ url: url_to_use.call(controller_path + "/:id"),
57
73
  method: "PUT",
58
74
  action_name: action_name,
59
75
  })
60
76
  when "destroy"
61
77
  auto_found_routes << base_opts.merge({
62
- url: controller_path + "/:id",
78
+ url: url_to_use.call(controller_path + "/:id"),
63
79
  method: "DELETE",
64
80
  action_name: action_name,
65
81
  })
82
+ when "index"
83
+ auto_found_routes << base_opts.merge({
84
+ url: url_to_use.call(controller_path),
85
+ method: "GET",
86
+ action_name: action_name,
87
+ })
66
88
  else
67
- tmp_controller_instance = controller_class.new("")
68
- tmp_controller_instance.send(action_name)
69
- is_root = tmp_controller_instance.is_root?
70
- custom_path = tmp_controller_instance.custom_path
71
89
  url_to_use = if is_root
72
90
  "/"
73
91
  elsif custom_path
@@ -76,7 +94,6 @@ module Jetski
76
94
  url_friendly_action_name = action_name.split("_").join("-")
77
95
  controller_path + "/#{url_friendly_action_name}"
78
96
  end
79
- custom_request_method = tmp_controller_instance.custom_request_method
80
97
 
81
98
  auto_found_routes << base_opts.merge({
82
99
  url: url_to_use,
@@ -1,3 +1,3 @@
1
1
  module Jetski
2
- VERSION = "0.4.7"
2
+ VERSION = "0.4.8"
3
3
  end
data/lib/jetski.rb CHANGED
@@ -6,8 +6,10 @@ require "rack"
6
6
  require "sqlite3"
7
7
 
8
8
  require_relative './jetski/version'
9
- require_relative './jetski/frontend/reactive_form'
10
9
  require_relative './jetski/helpers/view_helpers'
10
+ require_relative './jetski/helpers/route_helpers'
11
+ require_relative './jetski/helpers/delegatable'
12
+ require_relative './jetski/frontend/reactive_form'
11
13
  require_relative './jetski/base_controller'
12
14
  require_relative './jetski/router/shared_methods'
13
15
  require_relative './jetski/router/parser'
@@ -16,7 +18,6 @@ require_relative './jetski/autoloader'
16
18
  require_relative './jetski/server'
17
19
  require_relative './jetski/database/base'
18
20
  require_relative './jetski/model'
19
- require_relative './jetski/helpers/delegatable'
20
21
  require_relative './jetski/view_renderer'
21
22
 
22
23
  module Jetski
@@ -1,5 +1,5 @@
1
1
  class PagesController < Jetski::BaseController
2
+ route :home, root: true
2
3
  def home
3
- @root = true
4
4
  end
5
5
  end
@@ -4,7 +4,7 @@
4
4
  <meta charset="UTF-8">
5
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
6
  <title> APP_NAME </title>
7
- <link rel="icon" type="image/x-icon" href="/jetski-logo.png">
7
+ <%= favicon_tag "jetski-logo.png" %>
8
8
  </head>
9
9
  <body>
10
10
  <%= yield %>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jetski
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.7
4
+ version: 0.4.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Indigo Tech Tutorials
@@ -127,6 +127,7 @@ files:
127
127
  - lib/jetski/frontend/reactive_form.js
128
128
  - lib/jetski/frontend/reactive_form.rb
129
129
  - lib/jetski/helpers/delegatable.rb
130
+ - lib/jetski/helpers/route_helpers.rb
130
131
  - lib/jetski/helpers/view_helpers.rb
131
132
  - lib/jetski/model.rb
132
133
  - lib/jetski/router.rb