If I write a class in a file in lib, I can find it. If I write a module in the same file, I cannot find it. Did I miss something?
The files look like:
And the file contents are:
class Cool
end
module Flags
Countries = []
end
If I try to call them from a controller:
class MainController < ApplicationController
def index
Cool.new # works
Flags::Countries # uninitialized constant MainController::Flags
end
end
Might not a solution to your specific scenario, just a few relevant points:
Rails autoloading expects classes / modules to follow namespacing rules:
Flags::Countries -> <autoload paths>/flags/countries.rb
http://urbanautomaton.com/blog/2013/08/27/rails-autoloading-hell/
Don't think /lib/ is a standard autoloading path, so you'll need to ensure it's added into your path.
Related
This is my first post so please excuse the brevity. I'm learning crystal and trying to include a file which has a shared name with another file in another path. According to the documentation I can use require "path/to/modules/" and include all files within, but when I do this I and then use the imported class, it tells me the class does not exist...
Nothing in the documentation seems to work. Is there some addon module I need to get the paths to work properly or am I misunderstanding something or do I have some syntax error?
This is my code (Using the example on the home page)
require "http/server"
require "modules/HttpRequest"
server = HTTP::Server.new do |context|
context.response.print(request.result)
end
server.listen(80)
And the file HttpRequest.cr is in /modules/.
The error I get
Error in Server.cr:2: while requiring "modules/HttpRequest": can't find file 'modules/HttpRequest'
If you're trying to require a shard:
- Did you remember to run `shards install`?
- Did you make sure you're running the compiler in the same directory as your shard.yml?
require "modules/HttpRequest"
^
Using require "modules/HttpRequest" will look up modules/HttpRequest in CRYSTAL_PATH according to the require rules.
To require a file relative to the current file you need to use require "./modules/HttpRequest".
I've tried $this->viewBuilder()->templatePath(), but this only returns the prefix and controller name. (ex. Dashboard/Users)
The full path is more like /usr/local/var/www/mysite/vendor/vendorname/users/src/Template/Dashboard/Users
I've tried a few other things like Plugin::path($this->viewBuilder()->plugin()) to get part of that path, but I have yet to find any piece of code that will return the settings for what the src folder is called and what the Template folder is called.
I could hard code them as 'src' . DS . 'Template', but was hoping I'd find something that would work even if those were changed through some config setting somewhere. (Ideally there would just be a viewBuilder->absoluteTemplatePath() or something like it.)
You can retrieve possible template paths via App::path().
If you want to retrieve the template path for your Users plugin, then you could do
$templatesPath = current(\Cake\Core\App::path('Template', 'Users'));
This would give you something like
/usr/local/var/www/mysite/vendor/vendorname/users/src/Template/
It should be noted that this method doesn't necessarily return only a single path, it does so for plugin templates though.
If you need the path to an actual file, then you'll have to concatenate the remaining path segments on your own.
See also
API > \Cake\Core\App::path()
I am trying to make sprokets compile a single standalone js asset, so it will uglify and minify it and be part of the entire rails projects.
I need that this js to have a non-digest name, so it's will not change (i.e. embedded in other websites, etc)
I can't seem to force rails (4) /sprockets to do my bidding.
What I tried:
Adding the asset (script.js) in a misc folder unders assets/javascripts and not load it in the sprockets javascript manifest. While this keeps it in the project, it doesn't get uglified and minified, and doesn't get automatically loaded via asset-sync.
Tried adding another manifest called scripts-manifest.js to //= require script.js asset, to add its path in the precompile path in application.rb, but the problem is that rails 4 adds digest to all assets no matter what (doesn't work like that in rails 3)
Tried using https://github.com/alexspeller/non-stupid-digest-assets to add a non digest version of the asset. I may have done it incorrectly, as it doesn't work or do anything..
I add the initializer NonStupidDigestAssets.whitelist = ["script.js"] and tried putting it in app/assets/javascripts/misc and in public/ but it still won't work/
I have read that this gem should help in most cases, and I am sure I am doing something wrong with either path definition or no including it somewhere
One way to do this is to add an initializer that generates the compiled versions directly.
Add your js file to a subfolder in /app/assets/javascripts. Don't include this in application.js so it isn't added to the compiled assets.
Create an initializer in /config/initializers that uses uglify directly
output_file = "#{Rails.root}/public/public_script.js"
input_file = "#{Rails.root}/app/assets/javascripts/non_digest/public_script.js"
uglified = Uglifier.compile(File.read(input_file))
File.open(output_file, 'w') {|f| f.write(uglified) }
Include the public js file (in this example: /public/public_script.js) in your application layout
This way you have direct access to make custom changes to how uglify handles your js and the location of the file never changes for your external services accessing them.
I did all this locally and tested that it worked using the beta version of Rails 4.2
Just wanted to add my own solution based off Ken's answer.
I created non_digest.rb in config/initializers:
Dir["#{Rails.root}/app/assets/javascripts/non_digest/*"].each do |asset|
asset_name = File.basename(asset)
asset_output = "#{Rails.root}/public/external/#{asset_name}"
asset_uglified = Uglifier.compile(File.read(asset))
File.open(asset_output, 'w') {|a| a.write(asset_uglified) }
end
Don't forget to stub the file in javascripts/application.js. as we probably don't want it compiled with the rest of our JS and we can continue to use //= require_tree .:
//= stub non_digest/external_bookmarklet
the way you would do this with rails 4 is the following:
add it to the precompile list config.assets.precompile += %w(your_file_name.js)
make sure it's not referenced in application.js (directly or via require_tree)
symlink the digested file on deployment
read the manifest.yml to get the actual filename
ln -s digested-filename.js actual-filename.js
since rails 4, generation of non-digested assets has been removed (for good reasons) and this is a simple and straight forward way to implement the desired behavior.
I have a django model class with several images related to each instance.
Those images follow a certain pattern and can be determined by the name field of the model.
Those Images reside within the project static files folder.
So I have written a method for my model class to generate file paths for the images. It searches the static files folder for all files that follow the pattern *.jpg (the asterisks is necessary, because the filename has incrementing numbers).
Once it has found a file it transforms the absolute filesystem path into an url that is passed to a view and template via a list.
def getImages(self)
matches[]
for filename in fnmatch.filter(
os.listdir(os.path.join(settings.STATIC_ROOT_DIR,'images')), self.name + '*.jpg'):
matches.append(
os.path.join(settings.STATIC_URL, 'images', os.path.split(filename)[1]))
return matches
This method works fine, but doesn't leave me quite satisfied. Here are the reasons:
For development mode I am required to introduce a new variable called STATIC_ROOT_DIR, to obtain the path of the static files folder. I would like to use a consistent way to reference the static root folder for development and production. How can I achieve this? I would like to avoid development mode hackery as much as possible.
I have to build a URL by joining the static_url path with other strings that will eventually make up the URL for this static file. Is there a better way to construct URLs? Maybe some library function?
Last but not least: Is it good practice to do this in a model? Or is such a task better be done by a view?
There is a STATIC_ROOT variable in settings.py. Why not use it?
Personally, I follow your way - just concatenating paths. But just found a function for that:
from django.contrib.staticfiles.templatetags.staticfiles import static
print static('yourfile.jpg')
It works for me.
I think model is a good place for it. You store files in filesystem like you store model data in database. In other words, both of these are examples of storage which is a model level thing.
I would like to throw in another one:
Using STATIC_ROOT will break if you host your files externally.
You can use the django-storage-backend yourself (untested, just written):
from django.core.files.storage import get_storage_class
from django.conf import settings
def getImages(self)
static_storage = get_storage_class(settings.STATICFILES_STORAGE)()
directories, files = static_storage.listdir('images')
return [
static_storage.url('images/' + file)
for file in files
if file.startswith(self.name) and file.endswith('.jpg')
]
This will even return the correct URL if you use CachedStaticFileStorage or S3BotoStorage (from django-storages). And this will also be fine if you are in dev-mode.
I want to make a C++ application that can handle both C++ and Python plugin. For the C++ part i'm fine, but I have questions about Python plugins.
What I want to do, is to have a directory with all my python plugins, and the application will load all plugins located in this directory (like Sublime Text 2).
My problem is that I don't know how to "parse" a python script to get the name of every class that inherits from my plugin interface in order to create them.
Is there a way in boost.python to do that ? (I haven't found informations about it)
Does python have module variable I can use to do this ? (I'm not so
good with python)
Do I need to use a lexer like antlr ? ( seems heavy ...)
Do I need to have a "create" function like in C++ ? (Sublime Text
2 don't seems to need that)
Finally, do you know C++ application that handle Python plugin where I can check the code ?
Thanks ;)
This question is a bit loaded/unclear, but I'll give it a shot.
My problem is that I don't know how to "parse" a python script to get the name of every class that inherits from my plugin interface in order to create them.
This can be done somewhat easily with a python script; perhaps you can write one and call it from your C++ application. Here is a snippet of code that finds python scripts '*.py', imports them, and looks for classes that subclass a class called PluginInterface... not sure what you need to do after that, so I put a TODO there.
def find_plugins(directory):
for dirname, _, filenames in os.walk(directory): # recursively search 'directory'
for filename in filenames:
# Look for files that end in '.py'
if (filename.endswith(".py")):
# Assume the filename is a python module, and attempt to find and load it
### need to chop off the ".py" to get the module_name
module_name = filename[:-3]
# Attempt to find and load the module
try:
module_info = imp.find_module(module_name, [dirname])
module = imp.load_module(module_name, *module_info)
# The module loaded successfully, now look through all
# the declarations for an item whose name that matches the module name
## First, define a predicate to filter for classes from the module
## that subclass PluginInterface
predicate = lambda obj: inspect.isclass(obj) and \
obj.__module__ == module_name and \
issubclass(obj, PluginInterface)
for _, declaration in inspect.getmembers(module, predicate):
# Each 'declaration' is a class defined in the module that inherits
# from 'PluginInterface'; you can instantiate an object of that class
# and return it, print the name of the class, etc.
# TODO: fill this in
pass
except:
# If anything goes wrong loading the module, skip it quietly
pass
Perhaps this is enough to get you started, although it's not really complete, and you'll probably want to understand all the python libraries being used here so you can maintain this in the future.