Module: Toast::ActiveRecordExtensions

Defined in:
lib/toast/active_record_extensions.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_resource(&block) ⇒ Object

Configuration DSL



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/toast/active_record_extensions.rb', line 8

def acts_as_resource &block

  @toast_configs ||= Array.new
  @toast_configs << Toast::ConfigDSL::Base.new(self)

  Blockenspiel.invoke( block, @toast_configs.last)

  # add class methods
  self.instance_eval do

    def is_resourceful_model?
      true
    end

    def toast_configs
      @toast_configs
    end

    # get a config by media type or first one if none matches
    def toast_config media_type
      @toast_configs.find do |tc|
        tc.media_type == media_type || tc.in_collection.media_type == media_type
      end || @toast_configs.first
    end

    def toast_resource_uri_prefix
      @toast_resource_uri_prefix ||= self.to_s.pluralize.underscore
    end

  end

  # add instance methods
  self.class_eval do
    # Return the path segment of the URI of this record
    def uri_path
      "/" +
        self.class.toast_resource_uri_prefix + "/" +
        self.id.to_s
    end

    # Like ActiveRecord::Base.attributes, but result Hash includes
    # only attributes from the list _attr_names_ plus the
    # associations _assoc_names_ as links and the 'self' link
    def represent attr_names, assoc_names, base_uri, media_type
      props = {}

      attr_names.each do |name|
        unless self.respond_to?(name)
          raise "Toast Error: Connot find instance method '#{self.class}##{name}'"
        end
        props[name] = self.send(name)
      end

      assoc_names.each do |name|
        props[name] = "#{base_uri}#{self.uri_path}/#{name}"
      end

      props["self"] = base_uri + self.uri_path

      props
    end
  end
end

#is_resourceful_model?Boolean

defaults for non resourceful-models

Returns:

  • (Boolean)


73
74
75
# File 'lib/toast/active_record_extensions.rb', line 73

def is_resourceful_model?
  false
end

#resourceful_model_optionsObject



76
77
78
# File 'lib/toast/active_record_extensions.rb', line 76

def resourceful_model_options
  nil
end