-
Notifications
You must be signed in to change notification settings - Fork 92
Models
You can use a hobo generator to create a model
ruby script/generate hobo_model <model_name>
where should be the singular model_name instead of the class name ModelName or the plural model_names.
If the model is going to have views it needs a controller and you can create both model and controller at the same time by
ruby script/generate hobo_model_resource <model_name>
This creates a bunch of stuff
D:\workspace\TryHobo>ruby script\generate hobo_model_resource my_model
exists app/models/
exists app/controllers/
exists app/helpers/
create app/views/my_models
exists test/functional/
exists test/unit/
dependency hobo_model
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/my_model.rb
create test/unit/my_model_test.rb
create test/fixtures/my_models.yml
create app/controllers/my_models_controller.rb
create test/functional/my_models_controller_test.rb
create app/helpers/my_models_helper.rb
D:\workspace\TryHobo>cd app
D:\workspace\TryHobo\app>cd models
D:\workspace\TryHobo\app\models>dir
Volume in drive D is DATA
Volume Serial Number is 1440-4E18
Directory of D:\workspace\TryHobo\app\models
10/13/2008 03:02 PM <DIR> .
10/13/2008 03:02 PM <DIR> ..
10/09/2008 02:30 PM 202 guest.rb
10/13/2008 03:02 PM 377 my_model.rb
10/09/2008 02:45 PM 444 project.rb
10/09/2008 02:46 PM 1,712 user.rb
10/09/2008 02:30 PM 440 user_mailer.rb
6 File(s) 3,668 bytes
2 Dir(s) 176,363,552,768 bytes free
D:\workspace\TryHobo\app\models>
Let us look at my_model.rb
class MyModel < ActiveRecord::Base
hobo_model # Don't put anything above this
fields do
timestamps
end
# --- Hobo Permissions --- #
def creatable_by?(user)
user.administrator?
end
def updatable_by?(user, new)
user.administrator?
end
def deletable_by?(user)
user.administrator?
end
def viewable_by?(user, field)
true
end
end
Fields are defined here
fields do
name :string
timestamps
end
“name” is a good field to define in the model because if you define it, later you can reference a model with “this” and the convention will be to use the name field when creating links to the model record. Also <model>.to_s will return the name field value.
Here are some examples of different field definitions
fields do
name :string
required_string :string, :required
unique_string :string, :unique
in_use :boolean, :default => false
status enum_string(:new, :selected, :hidden), :default => :new
comments :text, :limit=>2000
publish_date :date
dt :datetime
amount :decimal, :precision => 8, :scale => 2
cycle_length :integer
timestamps
end
a boolean field will be stored as a tinyint(1) where 0=false and 1=true if using MySQL
enum_string lets you define static coded strings, this example will store the values “new”, “selected”, or “hidden” into a field named “status”
the amount field is defined as a fixed decimal number with 6 places before the decimal and 2 places after.
timestamps declares two :datetime fields updated_at and created_at which are automatically set to the current datetime when adding or updating records