Divs instead of Tables in Rails generator - ruby-on-rails

Is it possible to customize or find a gem that changes the behaviors of auto generating Tables in the views when I use rails scaffold and change it with Divs instead? Like cleaner templates, I'm using rayan's nifty generators but it uses tables instead of divs .
Any help would be highly appreciate .
Eqbal

I don't know of any such scaffold generator, but you can build one easily by taking Ryan's nifty generators as a starting point. You can modify it to your needs by following these steps:
Fork the repository on Github and clone it.
Change the file /rails_generators/nifty_scaffold/templates/views/erb/index.html.erb to fit your needs.
Commit and push your changes.
Add the newly created Gem to your Gemfile like this:
gem "nifty-generators", :gem => "https://github.com/[your_user]/nifty-generators.git"

Related

Creating a gem for cells components

I want to create UI components to enable users to edit data present in a specified structure. I decided to give Cells a try, but I need to encapsulate the whole code in a gem, since I want to provide this components as an extension to an existing gem (criteria_operator), which is the source of the classes used for the data.
Is this possible? And if yes, how do I start? Normally, cells expects you to create views and models in specific folders of the default rails folder structure. When creating a gem, I only have the lib folder...
If you need any additional information, please just point it out in the comments. I wasn't sure if there is anything useful I could provide.
Disclaimer: The gem mentioned was created by me, and this question isn't intended to promote it.
To use another gem- criteria_operator, just add it to your gemspec:
s.add_runtime_dependency('gem_name', '~> <version>')
or add it to your gems's Gemfile.
To have models and views in your gem, you can use rails engine.
Rails engine allow you to wrap a specific Rails application or subset of it inside a gem.

How do I undo Rails template generation in Haml?

I installed a new gem to my Rails project - 'phrasing' - which has gem dependencies on multiple Haml gems. I need the gem (it is adding some phenomenal functionality to my project), and need the dependencies to be installed, but now when I run rails generate scaffold, all of the view templates are generated in Haml rather than ERB. I need views to continue to be generated in ERB.
I reviewed several previous StackOverflow articles, but most focus on converting TO Haml, and the answers around making sure that ERB is the default generator refer to removing lines from the config files, which are not there in my case (it appears that the gems are controlling the default layout format somehow).
(Note: While I appreciate that there may be several advantages to using Haml, and I might consider using it in a new project, I'd like to keep this one consistent, and it's already 80% finished in ERB.)
Turns out this was as simple as adding the following into class Application in the config/application.rb file:
config.generators do |g|
g.template_engine :erb
end
Still not sure why the manual override was necessary, but it is. This fixes it.

How to apply bootstrap to existing rails file generated through scaffold?

through railscasts, I found http://railscasts.com/episodes/328-twitter-bootstrap-basics that with the
gem 'less-rails' (this is the gem https://github.com/seyhunak/twitter-bootstrap-rails)
I can use
rails g bootstrap:themed scaffold-name -f
which applies bootstap to various classes like table, buttons etc. This is amazing timesaver but I am not able to find this for SASS as I am using SASS for my rails app (I am using this gem https://github.com/thomas-mcdonald/bootstrap-sass)
IS there any way to apply bootstap to existing scaffold code as me doing this manually can take lot of time ? OR is there any easy way to put the classes in the .erb files ?
Thanks

How to create a custom scaffold generator in Rails 3?

There are these railscasts.
http://railscasts.com/episodes/218-making-generators-in-rails-3 With this one you find out how
to create a stylesheets and scaffold generator.
http://railscasts.com/episodes/216-generators-in-rails-3 With this one you find out how to add some files to modify the scaffolding views.
I want to do a mix of the two. I would like to create a generator that also creates scaffolding views. Kinda like Ryan Bates nifty generators or web_app_theme gem (https://github.com/pilu/web-app-theme). I have been searching for a tutorial or some information to point me in the right direction but I can't find exactly what I'm looking for.
I know I'm close. I already know how to create a generator with Railcast 218 but now, how can I make it create view files too?
I would like to run a command like this...
rails g my_scaffold_generator Post title:string body:text
This may well be too late to help, but as I found this while Googling for the same info...
It seems to me that the best approach, at least for learning the ropes, is to duplicate and then alter the existing scaffold generator.
So the first thing that tripped me up is finding the default templates, which do not live in your rails-3.2.0 directory (or whatever version you are on), but in railties-3.2.0. So for my RVM-based installation they were at:
/Users/leo/.rvm/gems/ruby-1.9.3-p194#gemset/gems/railties-3.2.0/lib/rails/generators/
[Note: your gems directory could be somewhere else entirely, use $> gem environment to find your gem paths]
In here is erb/scaffold/templates/ which has the files you'd expect (new.html.erb, _form.html.erb etc).
You can copy these files to your app's root, into lib/templates/erb/scaffold/ and they will be used instead of the default ones.
If you want to use these in a custom generator, there are two approaches:
1) Use hook_for to call the regular ERB scaffold generator from your generator.
2) Move/process the templates inside your own custom generator, using copy_file and similar methods in Thor to move them into place.
There is a decent Rails Guide on this, although I found I didn't really get it until I started digging around in .../railties-3.2.0/lib/rails/generators/... and looking at how the defaults are structured.

can i "freeze the code" of activescaffold or rails_admin in rails?

i want to generate a complex scaffold and then remove the gems
is there a way to freeze the code that rails_admin or activescaffold generates so i can edit it myself ? (similar to how rails scaffold does it)
is there another gem that generates a more complex scaffold?
In active scaffold to alter a scaffold's views you use overrides.
This depends on what version of Rails you're running. If >2.1, you can specify gems explicitly in environment.rb using config.gem, and then run rake gems:unpack to freeze those gems into the vendor/gems folder. For >=3.0 use the Builder tool to freeze the gem.
If <= 2.1, then you could do the above step manually -- copy all of the ActiveScaffold gem code into a folder in vendor/plugins, and remove the gem itself. See earlier plugin-based versions of ActiveScaffold for guidance.
You can also do this only as needed. To customize views, create an app/views/active_scaffold_overrides folder, and copy any ActiveScaffold partials to customize there. They will automatically be used across your app -- no need to duplicate them into each view. To customize controller actions, create a controller named ActiveScaffold, and then have all other scaffold controllers inherit from this new ActiveScaffoldController. Now you have somewhere to override the actions themselves, and you can override helpers in the generated ActiveScaffoldHelper file too.

Resources