Module: Bento::Generators::OrmHelpers

Included in:
ActiveRecord::Generators::BentoGenerator
Defined in:
lib/generators/bento/orm_helpers.rb

Instance Method Summary collapse

Instance Method Details

#bento_membership_exists?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/generators/bento/orm_helpers.rb', line 55

def bento_membership_exists?
  File.exists?(File.join(destination_root, bento_membership_path))
end

#bento_membership_model_contentObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/generators/bento/orm_helpers.rb', line 40

def bento_membership_model_content
<<-CONTENT
class BentoMembership < ActiveRecord::Base
  belongs_to :user
  belongs_to :#{class_name.underscore}

  attr_accessible :#{class_name.underscore}_id, :user_id
end
CONTENT
end

#bento_membership_pathObject



63
64
65
# File 'lib/generators/bento/orm_helpers.rb', line 63

def bento_membership_path
  @bento_membership_path ||= File.join("app", "models", "bento_membership.rb")
end

#model_contentsObject



4
5
6
7
8
9
10
11
12
13
# File 'lib/generators/bento/orm_helpers.rb', line 4

def model_contents
<<-CONTENT
  # Include all bento modules. Others available are:
  # :all, :validations, :user_accessors, :user_association, :user_accessors, :trial
  bento

  # Setup accessible (or protected) attributes for your model
  attr_accessible :name
CONTENT
end

#model_exists?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/generators/bento/orm_helpers.rb', line 51

def model_exists?
  File.exists?(File.join(destination_root, model_path))
end

#model_pathObject



59
60
61
# File 'lib/generators/bento/orm_helpers.rb', line 59

def model_path
  @model_path ||= File.join("app", "models", "#{file_path}.rb")
end

#user_model_contentObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/generators/bento/orm_helpers.rb', line 15

def user_model_content
<<-CONTENT
  has_many :bento_memberships
  has_many :#{table_name}, :through => :bento_memberships

  #
  # Remove this method call unless you want to work with
  # the has_many :through association as if it was a simple
  # belongs_to :#{class_name.underscore}.
  #
  # If you know for sure that you don't want to be able
  # to have users that belongs to several #{table_name} you
  # can of course edit the generated migration files to
  # not create the join model table "bento_memberships" and instead
  # add a #{class_name.underscore}_id to your User model.
  #
  # You also need to not use the :user_association module in bento
  # for example to use all in your #{class_name} model change the
  # call to bento from `bento :all` to bento `:validations, :user_accessors, :user_accessors, :trial`
  # then you just remove the above relations and remove the "fake_" part below.
  #
  fake_belongs_to :#{class_name.underscore}
CONTENT
end

#user_pathObject



67
68
69
# File 'lib/generators/bento/orm_helpers.rb', line 67

def user_path
  @user_path ||= File.join("app", "models", "user.rb")
end