gxapi_rails 0.0.2

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.
Files changed (68) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +20 -0
  3. data/README.md +161 -0
  4. data/Rakefile +25 -0
  5. data/app/helpers/gxapi_helper.rb +108 -0
  6. data/lib/gxapi.rb +160 -0
  7. data/lib/gxapi/base.rb +100 -0
  8. data/lib/gxapi/controller_methods.rb +41 -0
  9. data/lib/gxapi/engine.rb +14 -0
  10. data/lib/gxapi/google_analytics.rb +193 -0
  11. data/lib/gxapi/ostruct.rb +86 -0
  12. data/lib/gxapi/version.rb +3 -0
  13. data/spec/dummy/README.rdoc +261 -0
  14. data/spec/dummy/Rakefile +7 -0
  15. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  16. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  17. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  18. data/spec/dummy/app/controllers/posts_controller.rb +11 -0
  19. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  20. data/spec/dummy/app/helpers/posts_helper.rb +2 -0
  21. data/spec/dummy/app/models/post.rb +3 -0
  22. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  23. data/spec/dummy/app/views/posts/_form.html.erb +25 -0
  24. data/spec/dummy/app/views/posts/edit.html.erb +6 -0
  25. data/spec/dummy/app/views/posts/index.html.erb +36 -0
  26. data/spec/dummy/app/views/posts/new.html.erb +5 -0
  27. data/spec/dummy/app/views/posts/show.html.erb +15 -0
  28. data/spec/dummy/config.ru +4 -0
  29. data/spec/dummy/config/application.rb +70 -0
  30. data/spec/dummy/config/boot.rb +10 -0
  31. data/spec/dummy/config/database.yml +22 -0
  32. data/spec/dummy/config/database.yml.default +111 -0
  33. data/spec/dummy/config/environment.rb +5 -0
  34. data/spec/dummy/config/environments/development.rb +38 -0
  35. data/spec/dummy/config/environments/production.rb +67 -0
  36. data/spec/dummy/config/environments/test.rb +37 -0
  37. data/spec/dummy/config/gxapi.yml +17 -0
  38. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  39. data/spec/dummy/config/initializers/inflections.rb +15 -0
  40. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  41. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  42. data/spec/dummy/config/initializers/session_store.rb +8 -0
  43. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  44. data/spec/dummy/config/konfig/application.rb +16 -0
  45. data/spec/dummy/config/konfig/development.rb +10 -0
  46. data/spec/dummy/config/konfig/production.rb +3 -0
  47. data/spec/dummy/config/konfig/test.rb +3 -0
  48. data/spec/dummy/config/konfig/test_prod.rb +3 -0
  49. data/spec/dummy/config/locales/en.yml +5 -0
  50. data/spec/dummy/config/routes.rb +3 -0
  51. data/spec/dummy/config/test.p12 +0 -0
  52. data/spec/dummy/data/log/dummy/development.log +113 -0
  53. data/spec/dummy/data/log/dummy/test.log +927 -0
  54. data/spec/dummy/db/development.sqlite3 +0 -0
  55. data/spec/dummy/db/test.sqlite3 +0 -0
  56. data/spec/dummy/log/test.log +1443 -0
  57. data/spec/dummy/public/404.html +26 -0
  58. data/spec/dummy/public/422.html +26 -0
  59. data/spec/dummy/public/500.html +25 -0
  60. data/spec/dummy/public/favicon.ico +0 -0
  61. data/spec/dummy/script/rails +6 -0
  62. data/spec/features/display_variant_js_spec.rb +34 -0
  63. data/spec/helpers/gxapi_helper_spec.rb +63 -0
  64. data/spec/lib/gxapi/base_spec.rb +80 -0
  65. data/spec/lib/gxapi/google_analytics_spec.rb +70 -0
  66. data/spec/lib/gxapi_spec.rb +32 -0
  67. data/spec/spec_helper.rb +32 -0
  68. metadata +233 -0
data/lib/gxapi/base.rb ADDED
@@ -0,0 +1,100 @@
1
+ module Gxapi
2
+ class Base
3
+
4
+ TIMEOUT = 2.0
5
+
6
+ attr_reader :user_key
7
+
8
+ #
9
+ # @param user_key [String] identifier for our user - this is used in
10
+ # the cache_key
11
+ def initialize(user_key)
12
+ @user_key = user_key
13
+ @interface = GoogleAnalytics.new
14
+ end
15
+
16
+ # get the {Gxapi.env}
17
+ def env
18
+ Gxapi.env
19
+ end
20
+
21
+ #
22
+ # get all experiments
23
+ #
24
+ # @return [Array<Ostruct>]
25
+ def get_experiments
26
+ @interface.get_experiments
27
+ end
28
+
29
+ #
30
+ # return a variant value
31
+ #
32
+ # @example
33
+ # variant = @gxapi.get_variant("my_experiment")
34
+ # variant.value =>
35
+ # # Ostruct.new(experiment_id: "x", index: 1, name: "name")
36
+ #
37
+ # @return [Celluloid::Future]
38
+ def get_variant(experiment_name, override = nil)
39
+ Celluloid::Future.new do
40
+ # allows us to override and get back a variant
41
+ # easily that conforms to the api
42
+ if override.nil?
43
+ self.get_variant_value(experiment_name)
44
+ else
45
+ Ostruct.new(self.default_values.merge(name: override))
46
+ end
47
+ end
48
+ end
49
+
50
+ #
51
+ # reload the experiment cache from the remote
52
+ #
53
+ # @return [Boolean] true
54
+ def reload_experiments
55
+ @interface.reload_experiments
56
+ true
57
+ end
58
+
59
+ protected
60
+
61
+ #
62
+ # cache key for a given experiment and our user
63
+ #
64
+ # @param experiment_name [String] The name of our experiment
65
+ #
66
+ # @return [String] The cache key
67
+ def cache_key(experiment_name)
68
+ experiment_name = experiment_name.downcase.gsub(/\s+/,'_')
69
+ "#{Gxapi.cache_namespace}#{self.user_key}_#{experiment_name}"
70
+ end
71
+
72
+ #
73
+ # Default hash values for when a variant isn't found
74
+ #
75
+ # @return [Hash] Default values for when something goes wrong
76
+ def default_values
77
+ {name: "default", index: -1, experiment_id: nil}
78
+ end
79
+
80
+ #
81
+ # protected method to make the actual calls to get values
82
+ # from the cache or from Google
83
+ #
84
+ # @param experiment_name [String] Experiment name to look for
85
+ #
86
+ # @return [Gxapi::Ostruct] Experiment data
87
+ def get_variant_value(experiment_name)
88
+ data = Gxapi.with_error_handling do
89
+ Timeout::timeout(2.0) do
90
+ Gxapi.cache.fetch(self.cache_key(experiment_name)) do
91
+ @interface.get_variant(experiment_name).to_hash
92
+ end
93
+ end
94
+ end
95
+ Ostruct.new(
96
+ data.is_a?(Hash) ? data : self.default_values
97
+ )
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,41 @@
1
+ module Gxapi
2
+ module ControllerMethods
3
+
4
+ #
5
+ # Get the variant and set it as an instance variable, handling
6
+ # overriding by passing in the URL
7
+ #
8
+ # @param experiment_name [String] Name for the experiment
9
+ # @param ivar_name [String, Symbol] Name for the variable
10
+ #
11
+ # @return [Celluloid::Future, Gxapi::Ostruct] Variant value
12
+ def gxapi_get_variant(experiment_name, ivar_name = :variant)
13
+ # handle override
14
+ if params[ivar_name]
15
+ val = Gxapi::Ostruct.new(
16
+ value: {
17
+ index: -1,
18
+ experiment_id: nil,
19
+ name: params[ivar_name]
20
+ }
21
+ )
22
+ else
23
+ val = self.gxapi_base.get_variant(experiment_name)
24
+ end
25
+ return instance_variable_set("@#{ivar_name}", val)
26
+ end
27
+
28
+ protected
29
+
30
+ def gxapi_base
31
+ @gxapi_base ||= begin
32
+ Gxapi::Base.new(self.gxapi_token)
33
+ end
34
+ end
35
+
36
+ def gxapi_token
37
+ cookies[:gxapi] ||= SecureRandom.hex(16)
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,14 @@
1
+ if Rails::VERSION::MAJOR >= 3
2
+ module Gxapi
3
+ class Engine < Rails::Engine
4
+ config.after_initialize do
5
+ Gxapi.cache = Rails.cache
6
+ # add our helper
7
+ ApplicationHelper.send(:include, GxapiHelper)
8
+ ApplicationController.send(:include, Gxapi::ControllerMethods)
9
+ end
10
+ end
11
+ end
12
+ else
13
+ require File.expand_path('../../../app/helpers/gxapi_helper', __FILE__)
14
+ end
@@ -0,0 +1,193 @@
1
+ require 'google/api_client'
2
+
3
+ module Gxapi
4
+ class GoogleAnalytics
5
+
6
+ CACHE_KEY = "gxapi-google-analytics-experiments"
7
+
8
+ #
9
+ # Gets the experiment that has this name
10
+ #
11
+ # @param name [String] Name of the experiment
12
+ #
13
+ # @return [Gxapi::Ostruct]
14
+ def get_experiment(name)
15
+ self.get_experiments.find{|experiment| experiment.name == name}
16
+ end
17
+
18
+ #
19
+ # return a list of all experiments
20
+ #
21
+ # @return [Array<Gxapi::Ostruct>]
22
+ def get_experiments
23
+ @experiments ||= begin
24
+ # fetch our data from the cache
25
+ data = Gxapi.with_error_handling do
26
+ # handle caching
27
+ self.list_experiments_from_cache
28
+ end
29
+ # turn into Gxapi::Ostructs
30
+ (data || []).collect{|data| Ostruct.new(data)}
31
+ end
32
+ end
33
+
34
+ #
35
+ # get a variant for an experiment
36
+ #
37
+ # @param experiment_name [String]
38
+ #
39
+ # @return [Gxapi::Ostruct]
40
+ def get_variant(experiment_name)
41
+ # pull in an experiment
42
+ experiment = self.get_experiment(experiment_name)
43
+
44
+ if self.run_experiment?(experiment)
45
+ # select variant for the experiment
46
+ variant = self.select_variant(experiment)
47
+ # return if it it's present
48
+ return variant if variant.present?
49
+ end
50
+ # return blank value if we don't have an experiment or don't get
51
+ # a valid value
52
+ return Ostruct.new(
53
+ name: "default",
54
+ index: -1,
55
+ experiment_id: nil
56
+ )
57
+ end
58
+
59
+ #
60
+ # reset and return a list of all experiments
61
+ #
62
+ # @return Array [Gxapi::Ostruct]
63
+ def reload_experiments
64
+ Gxapi.cache.delete(CACHE_KEY)
65
+ self.get_experiments
66
+ end
67
+
68
+ protected
69
+
70
+ #
71
+ # Api definition for analytics api
72
+ #
73
+ # @return [Google::APIClient::API] Discovered Analytics endpoint
74
+ def analytics
75
+ @analytics ||= self.client.discovered_api('analytics', 'v3')
76
+ end
77
+
78
+
79
+ #
80
+ # Discovered definition of Analytics Experiments
81
+ #
82
+ # @return [Google::APIClient::API] Discovered Analytics endpoint
83
+ def analytics_experiments
84
+ self.analytics.management.experiments
85
+ end
86
+
87
+ #
88
+ # Accessor for Google Analytics config
89
+ #
90
+ # @return [Ostruct] Configuration
91
+ def config
92
+ Gxapi.config.google_analytics
93
+ end
94
+
95
+ #
96
+ # google api client
97
+ #
98
+ # @return [Google::APIClient]
99
+ def client
100
+ @client ||= begin
101
+ client = Google::APIClient.new
102
+ # key stuff is hardcoded for now
103
+ key = Google::APIClient::KeyUtils.load_from_pkcs12(
104
+ Gxapi.config.google.private_key_path, 'notasecret'
105
+ )
106
+ client.authorization = Signet::OAuth2::Client.new(
107
+ token_credential_uri: 'https://accounts.google.com/o/oauth2/token',
108
+ audience: 'https://accounts.google.com/o/oauth2/token',
109
+ scope: 'https://www.googleapis.com/auth/analytics.readonly',
110
+ issuer: Gxapi.config.google.email,
111
+ signing_key: key
112
+ )
113
+ client.authorization.fetch_access_token!
114
+ client
115
+ end
116
+ end
117
+
118
+ #
119
+ # List all experiments for our account
120
+ #
121
+ # @return [Array<Gxapi::Ostruct>] Collection of Experiment data
122
+ # retrieved from Google's API
123
+ def list_experiments
124
+ response = self.client.execute({
125
+ api_method: self.analytics_experiments.list,
126
+ parameters: {
127
+ accountId: self.config.account_id.to_s,
128
+ profileId: self.config.profile_id.to_s,
129
+ webPropertyId: self.config.web_property_id
130
+ }
131
+ })
132
+ response.data.items.collect(&:to_hash)
133
+ end
134
+
135
+ #
136
+ # List all experiments for our account, fetching from cache first
137
+ #
138
+ # @return [Array<Gxapi::Ostruct>] Collection of Experiment data from
139
+ # our cache or Google's API
140
+ def list_experiments_from_cache
141
+ Gxapi.cache.fetch(CACHE_KEY) do
142
+ self.list_experiments
143
+ end
144
+ end
145
+
146
+ #
147
+ # should we run this experiment under the current conditions?
148
+ #
149
+ # @return [Boolean] should_run
150
+ def run_experiment?(experiment)
151
+ # a blank experiment can't run
152
+ return false if experiment.nil?
153
+
154
+ # get a random value - a 100% coverage is represented
155
+ # as 1.0, so Kernel.rand works for us as its max is
156
+ # 1.0
157
+ return experiment.traffic_coverage >= Kernel.rand
158
+ end
159
+
160
+ #
161
+ # Select a variant from a given experiment
162
+ #
163
+ # @param experiment [Ostruct] The experiment to choose for
164
+ #
165
+ # @return [Ostruct, nil] The selected variant or nil if none is
166
+ # selected
167
+ def select_variant(experiment)
168
+ # starts off at 0
169
+ accum = 0.0
170
+ sample = Kernel.rand
171
+
172
+ # go through our experiments and return the variation that matches
173
+ # our random value
174
+ experiment.variations.each_with_index do |variation, i|
175
+
176
+ # we want to record the index in the array for this variation
177
+ variation.index = i
178
+ variation.experiment_id = experiment.id
179
+
180
+ # add the variation's weight to accum
181
+ accum += variation.weight
182
+
183
+ # return the variation if accum is more than our random value
184
+ if sample <= accum
185
+ return variation
186
+ end
187
+ end
188
+ # default to nil
189
+ return nil
190
+ end
191
+
192
+ end
193
+ end
@@ -0,0 +1,86 @@
1
+ module Gxapi
2
+ class Ostruct
3
+ # recursive open struct
4
+ def initialize(hash)
5
+
6
+ @hash = {}
7
+
8
+ self.send(:extend, self.generated_methods)
9
+ hash.each_pair do |key, val|
10
+ # set the key to an underscored verison of itself
11
+ key = self.underscore(key)
12
+ self.define_accessors(key)
13
+ self.send("#{key}=", self.convert_value_to_ostruct(val))
14
+ end
15
+ end
16
+
17
+ def to_hash
18
+ ret = {}
19
+ @hash.dup.each_pair do |key, val|
20
+ ret[key] = self.convert_value_from_ostruct(val)
21
+ end
22
+ ret
23
+ end
24
+
25
+ protected
26
+ # convert a value to a Ostruct (where necessary)
27
+ def convert_value_to_ostruct(data)
28
+ case data
29
+ when Hash
30
+ self.class.new(data)
31
+ when Array
32
+ data.collect{|val| self.convert_value_to_ostruct(val)}
33
+ else
34
+ data
35
+ end
36
+ end
37
+
38
+
39
+ def convert_value_from_ostruct(data)
40
+ case data
41
+ when Ostruct
42
+ data.to_hash
43
+ when Array
44
+ data.collect{|val| self.convert_value_from_ostruct(data)}
45
+ else
46
+ data
47
+ end
48
+ end
49
+
50
+ # define accessors for an attribute
51
+ def define_accessors(field)
52
+ # add the generated method
53
+ self.generated_methods.module_eval do
54
+ define_method(field) do
55
+ @hash[field]
56
+ end
57
+ define_method("#{field}=") do |val|
58
+ @hash[field] = val
59
+ end
60
+ end
61
+ end
62
+
63
+ # module to hold generated methods
64
+ def generated_methods
65
+ @generated_methods ||= Module.new
66
+ end
67
+
68
+ # dynamically define getter and setter when an unknown setter is called
69
+ def method_missing(meth, *args, &block)
70
+ if meth.to_s =~ /=$/
71
+ self.define_accessors(meth.to_s.gsub(/=$/,''))
72
+ return self.send(meth, *args, &block)
73
+ end
74
+ super
75
+ end
76
+
77
+ # take a string an convert it from
78
+ # camelCase to under_scored
79
+ def underscore(string)
80
+ string = string.to_s
81
+ string = string[0].downcase + string[1..-1].gsub(/([A-Z])/,'_\1')
82
+ string.downcase
83
+ end
84
+
85
+ end
86
+ end
@@ -0,0 +1,3 @@
1
+ module Gxapi
2
+ VERSION = '0.0.2'
3
+ end
@@ -0,0 +1,261 @@
1
+ == Welcome to Rails
2
+
3
+ Rails is a web-application framework that includes everything needed to create
4
+ database-backed web applications according to the Model-View-Control pattern.
5
+
6
+ This pattern splits the view (also called the presentation) into "dumb"
7
+ templates that are primarily responsible for inserting pre-built data in between
8
+ HTML tags. The model contains the "smart" domain objects (such as Account,
9
+ Product, Person, Post) that holds all the business logic and knows how to
10
+ persist themselves to a database. The controller handles the incoming requests
11
+ (such as Save New Account, Update Product, Show Post) by manipulating the model
12
+ and directing data to the view.
13
+
14
+ In Rails, the model is handled by what's called an object-relational mapping
15
+ layer entitled Active Record. This layer allows you to present the data from
16
+ database rows as objects and embellish these data objects with business logic
17
+ methods. You can read more about Active Record in
18
+ link:files/vendor/rails/activerecord/README.html.
19
+
20
+ The controller and view are handled by the Action Pack, which handles both
21
+ layers by its two parts: Action View and Action Controller. These two layers
22
+ are bundled in a single package due to their heavy interdependence. This is
23
+ unlike the relationship between the Active Record and Action Pack that is much
24
+ more separate. Each of these packages can be used independently outside of
25
+ Rails. You can read more about Action Pack in
26
+ link:files/vendor/rails/actionpack/README.html.
27
+
28
+
29
+ == Getting Started
30
+
31
+ 1. At the command prompt, create a new Rails application:
32
+ <tt>rails new myapp</tt> (where <tt>myapp</tt> is the application name)
33
+
34
+ 2. Change directory to <tt>myapp</tt> and start the web server:
35
+ <tt>cd myapp; rails server</tt> (run with --help for options)
36
+
37
+ 3. Go to http://localhost:3000/ and you'll see:
38
+ "Welcome aboard: You're riding Ruby on Rails!"
39
+
40
+ 4. Follow the guidelines to start developing your application. You can find
41
+ the following resources handy:
42
+
43
+ * The Getting Started Guide: http://guides.rubyonrails.org/getting_started.html
44
+ * Ruby on Rails Tutorial Book: http://www.railstutorial.org/
45
+
46
+
47
+ == Debugging Rails
48
+
49
+ Sometimes your application goes wrong. Fortunately there are a lot of tools that
50
+ will help you debug it and get it back on the rails.
51
+
52
+ First area to check is the application log files. Have "tail -f" commands
53
+ running on the server.log and development.log. Rails will automatically display
54
+ debugging and runtime information to these files. Debugging info will also be
55
+ shown in the browser on requests from 127.0.0.1.
56
+
57
+ You can also log your own messages directly into the log file from your code
58
+ using the Ruby logger class from inside your controllers. Example:
59
+
60
+ class WeblogController < ActionController::Base
61
+ def destroy
62
+ @weblog = Weblog.find(params[:id])
63
+ @weblog.destroy
64
+ logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!")
65
+ end
66
+ end
67
+
68
+ The result will be a message in your log file along the lines of:
69
+
70
+ Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1!
71
+
72
+ More information on how to use the logger is at http://www.ruby-doc.org/core/
73
+
74
+ Also, Ruby documentation can be found at http://www.ruby-lang.org/. There are
75
+ several books available online as well:
76
+
77
+ * Programming Ruby: http://www.ruby-doc.org/docs/ProgrammingRuby/ (Pickaxe)
78
+ * Learn to Program: http://pine.fm/LearnToProgram/ (a beginners guide)
79
+
80
+ These two books will bring you up to speed on the Ruby language and also on
81
+ programming in general.
82
+
83
+
84
+ == Debugger
85
+
86
+ Debugger support is available through the debugger command when you start your
87
+ Mongrel or WEBrick server with --debugger. This means that you can break out of
88
+ execution at any point in the code, investigate and change the model, and then,
89
+ resume execution! You need to install ruby-debug to run the server in debugging
90
+ mode. With gems, use <tt>sudo gem install ruby-debug</tt>. Example:
91
+
92
+ class WeblogController < ActionController::Base
93
+ def index
94
+ @posts = Post.all
95
+ debugger
96
+ end
97
+ end
98
+
99
+ So the controller will accept the action, run the first line, then present you
100
+ with a IRB prompt in the server window. Here you can do things like:
101
+
102
+ >> @posts.inspect
103
+ => "[#<Post:0x14a6be8
104
+ @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>,
105
+ #<Post:0x14a6620
106
+ @attributes={"title"=>"Rails", "body"=>"Only ten..", "id"=>"2"}>]"
107
+ >> @posts.first.title = "hello from a debugger"
108
+ => "hello from a debugger"
109
+
110
+ ...and even better, you can examine how your runtime objects actually work:
111
+
112
+ >> f = @posts.first
113
+ => #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
114
+ >> f.
115
+ Display all 152 possibilities? (y or n)
116
+
117
+ Finally, when you're ready to resume execution, you can enter "cont".
118
+
119
+
120
+ == Console
121
+
122
+ The console is a Ruby shell, which allows you to interact with your
123
+ application's domain model. Here you'll have all parts of the application
124
+ configured, just like it is when the application is running. You can inspect
125
+ domain models, change values, and save to the database. Starting the script
126
+ without arguments will launch it in the development environment.
127
+
128
+ To start the console, run <tt>rails console</tt> from the application
129
+ directory.
130
+
131
+ Options:
132
+
133
+ * Passing the <tt>-s, --sandbox</tt> argument will rollback any modifications
134
+ made to the database.
135
+ * Passing an environment name as an argument will load the corresponding
136
+ environment. Example: <tt>rails console production</tt>.
137
+
138
+ To reload your controllers and models after launching the console run
139
+ <tt>reload!</tt>
140
+
141
+ More information about irb can be found at:
142
+ link:http://www.rubycentral.org/pickaxe/irb.html
143
+
144
+
145
+ == dbconsole
146
+
147
+ You can go to the command line of your database directly through <tt>rails
148
+ dbconsole</tt>. You would be connected to the database with the credentials
149
+ defined in database.yml. Starting the script without arguments will connect you
150
+ to the development database. Passing an argument will connect you to a different
151
+ database, like <tt>rails dbconsole production</tt>. Currently works for MySQL,
152
+ PostgreSQL and SQLite 3.
153
+
154
+ == Description of Contents
155
+
156
+ The default directory structure of a generated Ruby on Rails application:
157
+
158
+ |-- app
159
+ | |-- assets
160
+ | |-- images
161
+ | |-- javascripts
162
+ | `-- stylesheets
163
+ | |-- controllers
164
+ | |-- helpers
165
+ | |-- mailers
166
+ | |-- models
167
+ | `-- views
168
+ | `-- layouts
169
+ |-- config
170
+ | |-- environments
171
+ | |-- initializers
172
+ | `-- locales
173
+ |-- db
174
+ |-- doc
175
+ |-- lib
176
+ | `-- tasks
177
+ |-- log
178
+ |-- public
179
+ |-- script
180
+ |-- test
181
+ | |-- fixtures
182
+ | |-- functional
183
+ | |-- integration
184
+ | |-- performance
185
+ | `-- unit
186
+ |-- tmp
187
+ | |-- cache
188
+ | |-- pids
189
+ | |-- sessions
190
+ | `-- sockets
191
+ `-- vendor
192
+ |-- assets
193
+ `-- stylesheets
194
+ `-- plugins
195
+
196
+ app
197
+ Holds all the code that's specific to this particular application.
198
+
199
+ app/assets
200
+ Contains subdirectories for images, stylesheets, and JavaScript files.
201
+
202
+ app/controllers
203
+ Holds controllers that should be named like weblogs_controller.rb for
204
+ automated URL mapping. All controllers should descend from
205
+ ApplicationController which itself descends from ActionController::Base.
206
+
207
+ app/models
208
+ Holds models that should be named like post.rb. Models descend from
209
+ ActiveRecord::Base by default.
210
+
211
+ app/views
212
+ Holds the template files for the view that should be named like
213
+ weblogs/index.html.erb for the WeblogsController#index action. All views use
214
+ eRuby syntax by default.
215
+
216
+ app/views/layouts
217
+ Holds the template files for layouts to be used with views. This models the
218
+ common header/footer method of wrapping views. In your views, define a layout
219
+ using the <tt>layout :default</tt> and create a file named default.html.erb.
220
+ Inside default.html.erb, call <% yield %> to render the view using this
221
+ layout.
222
+
223
+ app/helpers
224
+ Holds view helpers that should be named like weblogs_helper.rb. These are
225
+ generated for you automatically when using generators for controllers.
226
+ Helpers can be used to wrap functionality for your views into methods.
227
+
228
+ config
229
+ Configuration files for the Rails environment, the routing map, the database,
230
+ and other dependencies.
231
+
232
+ db
233
+ Contains the database schema in schema.rb. db/migrate contains all the
234
+ sequence of Migrations for your schema.
235
+
236
+ doc
237
+ This directory is where your application documentation will be stored when
238
+ generated using <tt>rake doc:app</tt>
239
+
240
+ lib
241
+ Application specific libraries. Basically, any kind of custom code that
242
+ doesn't belong under controllers, models, or helpers. This directory is in
243
+ the load path.
244
+
245
+ public
246
+ The directory available for the web server. Also contains the dispatchers and the
247
+ default HTML files. This should be set as the DOCUMENT_ROOT of your web
248
+ server.
249
+
250
+ script
251
+ Helper scripts for automation and generation.
252
+
253
+ test
254
+ Unit and functional tests along with fixtures. When using the rails generate
255
+ command, template test files will be generated for you and placed in this
256
+ directory.
257
+
258
+ vendor
259
+ External libraries that the application depends on. Also includes the plugins
260
+ subdirectory. If the app has frozen rails, those gems also go here, under
261
+ vendor/rails/. This directory is in the load path.