How can helpers be accessed from middleman console? Answer for both 3.x and 4.x would be helpful if it's not the same object path.
I couldn't find any documentation on this, so settled for the following work around:
Here's my helper
module PageHelpers
def thing
"THING!"
end
end
Call the helper method on an instance of a class that includes it:
$ bundle exec middleman console
irb...> (class Thing; include PageHelpers; end).new.thing
=> "THING!"
Related
I have two separate Rails applications
one just exposes an API (henceforth termed as API-app) and
another one acts as an Administration Portal (henceforth termed as AdminPortal) for managing the data in API-app.
Both are presently running on Rails 4.2.5.1 on staging as well as production environments. However on a staging_experimental environment I upgraded both the applications to Rails 5.0.0.1. I have rspecs for the both the applications and the spec suite in both the applications is passing and Capistrano deployment goes success. However when trying to send request to API-endpoint in API-app I face a runtime error
NameError (uninitialized constant Api::BaseController::ApiTokenAuthentication)
/lib/api_token_authentication.rb
module ApiTokenAuthentication
<some module_functions defined here>
end
and the module functions are accessed in my controller in following manner:
class Api::BaseController < ActionController::Base
def authenticate!(user)
ApiTokenAuthentication.my_module_function_here(user)
end
end
The same kind of runtime-error NameError (uninitialized constant ...) errors are also encountered in AdminPortal too and they refer to the code placed in AdminPortal's codebase's lib folder. For e.g.
NameError (uninitialized constant MyApiApp::Api):
NameError (uninitialized constant MyApiApp::Api::Get):
NameError (uninitialized > constant MyApiApp::Api::Post):
/lib/my_api_app
/lib/my_api_app/api.rb
module MyApiApp
module Api
end
end
/lib/my_api_app/api/get.rb
module MyApiApp
module Api
class Get < BaseRequest
end
end
end
/lib/my_api_app/api/post.rb
module MyApiApp
module Api
class Post < BaseRequest
end
end
end
/lib/my_api_app/api/base_request.rb
module MyApiApp
module Api
class BaseRequest
end
end
end
/lib/my_api_app/api/base_response.rb
module MyApiApp
module Api
class BaseResponse
end
end
end
Searching for a solution to fix these errors I found following post which mentioned to explicitly require the path in an initializer file. For e.g.
/config/initializers/load_my_api_app_api_utility_classes.rb
require File.join(Rails.root, "lib/my_api_app/api.rb")
lib_api_dir_path = Rails.root.join("lib", "my_api_app", "api").to_s
Dir["#{lib_api_dir_path}/*.rb"].each {|file| require file }
After adding the above initializer the errors in AdminPortal app went away
NameError (uninitialized constant MyApiApp::Api):
NameError (uninitialized constant MyApiApp::Api::Get):
NameError (uninitialized > constant MyApiApp::Api::Post):
but as soon as AdminPortal app sent request to the end-point exposed by API-app I started encountering following error (mentioned previously)
NameError (uninitialized constant Api::BaseController::ApiTokenAuthentication)
So I think there is some basic thing which is missing and which is causing the errors related to auto-loading of code residing in the lib folder and which is used by the controller classes. I am really puzzled on why this is happening because I have checked /config/application.rb and in both the applications config.autoload_paths << Rails.root.join('lib') is already there.
The same code is working without any issues on staging and production envs wherein the application is running on Rails 4.2.5.1.
Also not facing these errors with the upgraded code on development environment on my local machine.
Can anybody please shed light on this and guide me towards the resolution of this weird error?
Update - Oct 19, 2016
I received following answer here to my questions above
Autoload is disabled in any environment that has eager_load enabled.
You should never add lib to autoload_paths. Use eager_autoload_paths.
All this information is being explained in the upgrading guide
http://guides.rubyonrails.org/upgrading_ruby_on_rails.html#autoloading-is-disabled-after-booting-in-the-production-environment
I tried using the eager_load_paths approach but it started throwing new errors. As there are lot of details I have to share after using eager_load_paths, I have added them to a gist here
Hoping to receive some guidance soon.
Thanks.
Finally I found that there is no need neither to add anything to autoload_paths nor to eager_load_paths nor to explicitly add config.enable_dependency_loading = true to /config/environments/.rb
The reason being the utility code, which was supposed to be eager-loaded on production-kind environments i.e. production, staging etc is moved to a directory under app folder and as mentioned at https://stackoverflow.com/a/19852844/936494
Another interesting quirk is that in every rails app, all directories under app/ are
automatically in both autoload_paths and eager_load_paths, meaning that adding a directory there requires no further actions.
This way we automatically get rid of runtime errors faced like NameError (uninitialized constant MyNamespace::Api).
Thanks.
I am writing a small project in Elixir, where I will use the built in configuration capability. The way it looks like I have a general project that will call APIs:
api/confix.exs:
use Mix.Config
config :api, :status, "awesome"
I now have a second project that should utilize these variables
api_consumer/mix.exs
def application do
[applications: [:logger, :api]]
end
When I run a console in api_consumer accessing the variable yields a nil result.
iex -S mix
iex(1)> Application.get_env(:api, :status)
=> nil
From what I understand (and from what I read here) that should work.
Does anybody know what's going on here?
mix.exs is used to configure the current application, while config.exs is used to configure other applications. In your :api application, you should put the default values in the application/0 function inside mix.exs:
# api/mix.exs
def application do
[
applications: [:logger, :api],
env: [status: "awesome"]
]
end
Then, you can override this setting in your :api_consumer application inside the config.exs file:
# api_consumer/config/config.exs
config :api, status: "fantastic"
More info can be found here.
I would like to know how to use middleman console. Is it a simple irb? What can I do with it that differs from the simple irb?
middleman console [options] # Start an interactive console in the cont...
I have some articles and I try to do Article.all but I had this following error:
NameError: uninitialized constant Middleman::Application::MiddlemanApplication1::Article
from (irb#1):1
and I also have local-data /data/friends.json but Friend.all output error too.
{
"friends": [
"Tom",
"Dick",
"Harry"
]
}
I saw that Symbol.all_symbols output a lots of middleman variable and functions but I don't really know how to use the middleman console.
Oh man, I just spend a few hours trying to solve something that would have gone a lot faster if I had known that there was a middleman console!
I was trying to build the next/back logic for going through articles on my blog. There's a chronological thing, but I wanted to also be able to go back and forth on tags. There are a few that are tagged both "ruby" and (say) "rails", and "the next" for both tags was the same I wanted to list it together under both the tags.
The main problem was that I didn't know what sorts of things were available to me in the templates file. When you startup middleman console it loads up the configuration of your site so then you can start poking around.
$ bundle exec irb
2.0.0-p481 :001 > blog
NameError: undefined local variable or method `blog' for main:Object
from (irb):1
2.0.0-p481 :002 > exit
$ middleman console
== LiveReload is waiting for a browser to connect
2.0.0-p481 :001 > blog
=> #<Middleman::Blog::BlogData: [#<Middleman::Blog::BlogArticle: {"title"=>"Emb
etc.
What middleman console does is load up the middleman environment, and then lets you call methods on the current Middleman::Application object. If you are using a middleman extension and they've defined helpers, you can get to them here and start poking around.
Handy things:
config is the middleman config object.
data is the middleman data object, from the data directory
blog is the blog config, if you are using middleman-blog
drafts are the draft articles, if you are using middleman-blog-drafts
I'm using play application (using Play version 2.1.0) with RabbitMQ and do not have any view component.
So i would like to invoke this play application without hitting the execution URL (http://localhost:9000/<routing_info>) on server startup.
Would also like to know if there is any way in Play 2.1.0 version to run the application on server startup, i mean bootstrapping. Is this option available in play 2.1.0.
As i've read through the documentation its mentioned only for 1.2 version.
Please help!!
Play allows you to define a 'global' object which will be instantiated automatically by Play when the application starts.
In application.conf you should find the following:
# Global object class
# ~~~~~
# Define the Global object class for this application.
# Default to Global in the root package.
application.global=global.Global
On a new play application, this line is commented out. I've uncommented it and made it point to an object called Global in the global package. You can make it what ever you want.
Your global object should extend GlobalSettings.
In my applications, I use a static initialiser block to run code when that class is loaded:
public class Global extends GlobalSettings
{
static
{
...
}
}
I have a cucumber step
When /^I go to the Add Suggestions form$/ do
visit new_manage_suggestions_path
end
and a route
namespace "manage" do
resource :suggestions
end
rake routes outputs
manage_suggestions POST /manage suggestions(.:format) manage/suggestions#create
When I run cucumber I get
undefined method `suggestions_path' for #<#<Class:0x000000064a4768>:0x000000064accd8> (ActionView::Template::Error)
Why is cucumber trying that path?
The new_manage_suggestions_path works fine in my app, I have a link that uses it and that is working fine.
In your routes definition, in order to have your app generate the correct routes, you need to switch from the singular resource to plural resources since you could potentially have multiple suggestions.
namespace "manage" do
resources :suggestions
end
More details can be found in the Rails documentation on singular resources, where you can see that the singular version does not include the namespace in its path names.