You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/hyper-model/README.md
+13-43
Original file line number
Diff line number
Diff line change
@@ -72,34 +72,7 @@ In order for Hyperstack to see your Models \(and make them Isomorphic\) you need
72
72
|**Location of Models**|**Scope**|
73
73
| :--- | :--- |
74
74
|`app\models`| Server-side code only |
75
-
|`app\Hyperstack\models`| Isomorphic code \(client and server\)|
76
-
77
-
### Rails 5.1.x
78
-
79
-
Upto Rails 4.2, all models inherited from `ActiveRecord::Base`. But starting from Rails 5, all models will inherit from `ApplicationRecord`.
80
-
81
-
To accommodate this change, the following file has been automatically added to models in Rails 5 applications.
82
-
83
-
```ruby
84
-
# app/models/application_record.rb
85
-
classApplicationRecord < ActiveRecord::Base
86
-
self.abstract_class =true
87
-
end
88
-
```
89
-
90
-
For Hyperstack to see this change, this file needs to be moved \(or copied if you have some server-side models\) to the `apps/Hyperstack` folder.
91
-
92
-
### Explicit Scope Access
93
-
94
-
In order to prevent unauthorized access to information like scope counts, lists of record ids, etc, Hyperstack now \(see issue [https://github.com/ruby-Hyperstack/hyper-mesh/issues/43](https://github.com/ruby-Hyperstack/hyper-mesh/issues/43)\) requires you explicitly allow scopes to be viewed on the client, otherwise you will get an AccessViolation.
95
-
96
-
To globally allow access to all scopes add this to the ApplicationRecord class
97
-
98
-
```ruby
99
-
classApplicationRecord < ActiveRecord::Base
100
-
regulate_scope :all
101
-
end
102
-
```
75
+
|`app\hyperstack\models`| Isomorphic code \(client and server\)|
103
76
104
77
## ActiveRecord API
105
78
@@ -109,15 +82,15 @@ Hyperstack uses a subset of the standard ActiveRecord API to give your Isomorphi
109
82
110
83
Hyperstack integrates with React \(through Components\) to deliver your Model data to the client without you having to create extra APIs or specialized controllers. The key idea of React is that when state \(or params\) change, the portions of the display effected by this data will be updated.
111
84
112
-
Hyperstack automatically creates React state objects that will be updated as server side data is loaded or changes. When these states change the associated parts of the display will be updated.
85
+
On the client each database record being used by the client is represented as an observable store **([see the chapter on HyperState for details](hyper-state/README.md))** which will mutate as server side data is loaded or changes. When these states change the associated parts of the display will be updated.
113
86
114
-
A brief overview of how this works will help you understand the how Hypeloop gets the job done.
87
+
A brief overview of how this works will help you understand the how HyperStack gets the job done.
115
88
116
89
#### Rendering Cycle
117
90
118
91
On the UI you will be reading models in order to display data.
119
92
120
-
If during the rendering of the display the Model data is not yet loaded, placeholder values \(the default values from the `columns_hash`\) will be returned by Hyperstack.
93
+
If during the rendering of the display the Model data is not yet loaded, placeholder values \(the default values from the database schema\) will be returned by Hyperstack.
121
94
122
95
Hyperstack then keeps track of where these placeholders \(or `DummyValue`s\) are displayed, and when they do get loaded, those parts of the display will re-render.
123
96
@@ -143,7 +116,7 @@ There are a number of methods that allow you to interact with this load cycle wh
143
116
144
117
#### New and Create
145
118
146
-
`new`: Takes a hash of attributes and initializes a new unsaved record. The values of any attributes not specified in the hash will be taken from the Models default values specified in the `columns_hash`.
119
+
`new`: Takes a hash of attributes and initializes a new unsaved record. The values of any attributes not specified in the hash will be taken from the Models default values specified in the data base schema.
147
120
148
121
If `new` is passed a native javascript object it will be treated as a hash and converted accordingly.
149
122
@@ -209,7 +182,7 @@ Word.offset(500).limit(20) # get words 500-519
209
182
210
183
#### Applying Class Methods to Collections
211
184
212
-
Like Rails if you define a class method on a model, you can apply it to collection of those records, allowing you
185
+
Like Rails if you define a class method on a model, you can apply it to a collection of those records, allowing you
213
186
to chain methods with scopes (and relationships)
214
187
215
188
```ruby
@@ -264,6 +237,9 @@ class User < ActiveRecord::Base
264
237
end
265
238
```
266
239
240
+
241
+
242
+
267
243
Sometimes it is desirable to only run the method on the server. This can be done using the `server_method` macro:
268
244
269
245
```ruby
@@ -284,7 +260,7 @@ To force the value to be recomputed at the server append a `!` to the end of the
284
260
285
261
`columns_hash`: returns the details of the columns specification. Note that on the server `columns_hash` returns a hash of objects specifying column information. On the client the entire structure is just one big hash of hashes.
286
262
287
-
`abstract_class=`, `abstract_class?`, `primary_key`, `primary_key=`, `inheritance_column`, `inheritance_column=`, `model_name`, `serialize`, `alias_attribute`, `table_name`: All work as on the server. See ActiveRecord documentation for more info.
263
+
`abstract_class=`, `abstract_class?`, `primary_key`, `primary_key=`, `inheritance_column`, `inheritance_column=`, `model_name`: All work as on the server. See ActiveRecord documentation for more info.
288
264
289
265
### Instance Methods
290
266
@@ -313,10 +289,6 @@ end
313
289
314
290
After a save operation completes the models will have an `errors` hash \(just like on the server\) with any validation problems.
315
291
316
-
`save` does not fail, but rather returns `{success: false ...}`. If there was an
317
-
exception the `message` key will hold the error information, otherwise the errors will
318
-
be stored in the records error data (as on the server.)
319
-
320
292
During the save operation the method `saving?` will return `true`. This can be used to instead of \(or with\) the promise to update the screen:
321
293
322
294
```ruby
@@ -337,9 +309,7 @@ end
337
309
338
310
Like `save` destroy returns a promise that is resolved when the destroy completes.
339
311
340
-
After the destroy completes successfully the record's `destroyed?` method will return true.
341
-
342
-
Like `save` destroy never fails, but rather returns `{success: false...}`
312
+
After the destroy completes the record's `destroyed?` method will return true.
343
313
344
314
#### Other Instance Methods
345
315
@@ -397,12 +367,12 @@ The `load` method takes a list of attributes \(symbols\) and will insure these a
397
367
before_mount do
398
368
Todo.find(1).load(:name).then do |name|
399
369
@name= name;
400
-
mutate @loaded=true
370
+
state.loaded!true
401
371
end
402
372
end
403
373
```
404
374
405
-
> Think hard about how you are using this, as Hyperstack already acts as flux store, and is managing state for you. It may be you are just creating a redundant store!
375
+
Think hard about how you are using this, as Hyperstack already acts as flux store, and is managing state for you. It may be you are just creating a redundant store!
0 commit comments