I'm trying to override view with Deface.
In manual creators called it standalone, so I guess that it's possible to use this gem without using spree... But I'm not sure anymore because nearly every tutorial/question/anything I found about Deface was related to spree.
However I tried. what I done was adding a line to Gemfile
gem 'deface'
and running
bundle install
which for sure installed deface gem.
Then I made route like this:
get 'test', to: 'test#show'
empty controller app/controllers/test_controller.rb
class TestController < ApplicationController
def show
end
end
and view like this app/views/test/show.html/erb
<h1 id="test">test first</h1>
then I go to cd app and make directory overrides mkdir overrides and created app/overriders/test_uploader.rb which contains:
Deface.Override.new(
:virtual_path => 'test/show',
:name => 'test uploader',
:replace => 'h1#test',
:text => '<h1 id="test">replaced, test passed</h1>'
)
But even if I reloaded server nothing happened.
Why? Had I missed something? Or maybe I really needs spree to use Deface?
Not sure why your code sample not working but I did simple sample also for you and its works very well. You can download code project here https://github.com/nezirz/deface_gem
Also here is a screen shoot:
Related
I need to be able to see all of the class descendants from a controller when I go into the rails console locally. I have this Api::BaseController which all my Api controller inherit from. The issue I have is when I hop in to the rails console to check which Api controller are in the descendants, it comes up empty until I call them. This probably has something to do with how the classes in development aren't eager loaded, and they're not cached locally.
sample_app$ rails c
Loading development environment (Rails 4.2.0)
2.1.5 :001 > Api::BaseController.descendants
=> []
2.1.5 :002 > Api::V1::FoosController
=> Api::V1::FoosController
2.1.5 :003 > Api::BaseController.descendants
=> [Api::V1::FoosController]
From this example, you can see when I call descendants on the Api::BaseController the first time, it's an empty array. After calling one of the controllers that class will be then loaded and will show up as a descendant. In this case, there could be an any number of controllers in V1 as well as V2, V3, etc...
As a stupid ugly hack, I could do
Dir.glob("#{Rails.root.join('app', 'controllers', 'api', 'v1')}/**/*.rb").each(&method(:require_dependency))
but I don't want to have to write that each time I enter the console. I'm also working on a gem, and definitely don't want to put this sort of code in my gem.
The other option is caching classes in development, but that causes a huge issues on it's own. Anyone have any ideas?
Edit
Another option would be to call Rails.application.eager_load!. This option would work fine if I could specify only controllers in my API folder. Then I wouldn't have to eager load the entire app, but just a small subset of controllers that I need.
I found the following post: http://avinmathew.com/using-rails-descendants-method-in-development/
In short it says the following:
In enviroments/development.rb add the following:
config.eager_load_paths += Dir['path/to/files/*.rb']
ActionDispatch::Reloader.to_prepare do
Dir['path/to/files/*.rb'].each {|file| require_dependency file}
end
The first line adds the path that should be loaded when you start your app (or console)
and the rest tells rails to reload the classes on each request.
Works in Rails 5 & Rails 6:
In environments/development.rb:
Rails.application.reloader.to_prepare do
Dir["#{Rails.root}/app/models/my_models/*.rb"].each { |file| require_dependency file }
end
This builds on u/Aguardientico's answer above.
If requiring files not in config.eager_load_paths you may need to add yours as described above.
ActionDispatch::Reloader is replaced with Rails.application.reloader (thanks to u/jean-baptiste's comment).
#{Rails.root} was necessary as part of my argument to Dir for this to work.
I'm new to Ruby on Rails and I'm looking at an application that has a variable called current_teacher. I cannot seem to find where this is set. Everywhere I look the code seems to read from it but where is it set. Is this one of those things that Rails does for you. There is a mode and a table called teachers, so I'm sure this has something to do with it.
I'm very confused by statements like the following, can someone tell me how Rails does this?
if current_teacher.can_request_fieldtrip
Suppose you have a controller like :
class ClientsController < ApplicationController
def new
if current_teacher.can_request_fieldtrip
# code
end
end
end
Here is debugging tips :
(a) put this in your Gemfile and do bundle install :
`gem 'pry-rails', :group => :development`
(b) Put the line binding.pry just before the if statement.
(c) Start rails server using rails s.
(d) Hit the browser like http://localhost:3000/new
(e) Now you will be in the Pry console. Just do in the console,
method(:current_teacher).source_location
And the above line tell you where the method has been defined.
Documentation of Method#source_location
Returns the Ruby source filename and line number containing this method or nil if this method was not defined in Ruby (i.e. native)
Rails does not support authentication by itself, however there are a lot of 'add-ons' that rails can use. These 'add-ons' are called gems. This can be a little confusing because you can't actually see their code inside your project folder.
If you open a file called "Gemfile" (it should be in your project folder) you can see a list of gems that you use. Try searching their names on google, you will probably find official web page that contains it's documentation. That way can learn what they do and how to use them.
current_teacher method smells like "Devise" gem
https://github.com/plataformatec/devise
I'm not sure about can_request_fieldtrip, this could be a custom method defined in Teacher model.
I know, this question was asked at least milion times, but I still can't find the right solution. I mean clean and easy, like it is supposed to be in Rails applications.
Let's imagine I am developer of an application that needs some code in the lib folder. My app's name is Xy. I will create a file xy.rb in the lib folder with this content:
require 'xy/version'
module Xy
end
After that I will create next file in lib/xy/ named version.rb:
module Xy
module VERSION
def self.to_s
"1.0.0"
end
end
end
So far, so good. Then I will create some controller and I will edit routes:
class IndexController < ApplicationController
def index
render text: Xy::VERSION
end
end
root 'index#index'
I also need require my code, in application.rb I will add require 'xy'
Then I will run my server rails s and I will wisit the app in the browser http://localhost:3000 - seems good, I see 1.0.0.
Then I will update the version string in Xy::VERSION to 1.0.1 and I'll refresh the browser - here it comes, I will no see the change. To see it I will have to restart the server. And this is quite anoying.
So the question is: What is the right way to force RoR 4+ to reload changes in the lib folder in development environment?
Thank you in advance.
Add this to application.rb:
config.autoload_paths += %W(#{config.root}/lib)
I'm using this in my app now, you never need to even require' the lib files anywhere and don't have to restart the server. Just make sure you name your files correctly. E.g
/lib/foo/rb
class Foo
/lib/foo/bar.rb
class Foo::Bar
I am trying to use Deface gem for customizing a view that is located in gem (it is not spree).
The path to the view is:
my_gem/backend/layout/sidebar
When I run:
rake deface:test_selector['my_gem/backend/layout/sidebar','.mainnav ul']
I get two matches, the view and proper matching elements are correctly displayed.
However when I run:
rake deface:get_result['my_gem/backend/layout/sidebar']
nothing is changed in the view and apart from its code,"Overrides(0) found" is displayed.
My Override is located in: /app/overrides/add_slider_to_sidebar.rb file:
Deface::Override.new(:virtual_path => 'my_gem/backend/layout/sidebar',
:name => 'add_slider_to_sidebar',
:insert_bottom => ".mainnav ul",
gem:text => "<li>AAAAA</li>")
What should I change (or add to configuration) to allow deface to find my override?
I am using rails 4.0.5, deface 1.0.0 and nokogiri 1.6.2.1.
Edit:
I've made following tests:
Add deface to the application, and do the change mentioned above. Everything worked fine.
Add deface to the gem - so test in dummy application. The behavior described above has occurred.
Add gem with deface view modifications from point 2 but to regular application, not the dummy one. The above behavior has occurred.
I have an application with Rails 2.3.5. And Im trying to use AS latest version, I have used it previously but cant make it work here.
I have my ingredient_categories Controller, where i put
class Administration::IngredientCategoriesController < ApplicationController
layout "default"
active_scaffold :ingredient_categories
end
I have this set up on routes to be :active_scaffold=>true
I have a model also called ingredient_category, and in the views folder (inside administration/ingredient_categories, and /ingredient_categories) i have nothing as it is usual.
And Im getting over and over again:
Template is missing
Missing template ingredient_categories/list.html in view path themes/aqueouslight:app/views
I had an error before asking me for a list.erb, which I created and put
'ingredient_categories', :label => 'Categorias' %>
And now this error of the list.thml...
Cant make it work! dont know whhy really... whould be SO simple and its burning my head now..
Thanks!
Make sure you have the render_component plugin installed, as it was deprecated from rails 2.3.x and is used by active scaffold
This may not be the cause of your particular problem but you will have trouble with it if you combine AS+Rails2.3.x
I hope it helps