I have the following structure:
modules/group
modules/group/modules/forum
modules/group/modules/gallery
modules/group/modules/events
Is this a correct way of structuring groups? or is better
modules/group
modules/group/controllers/ForumController.php
modules/group/controllers/GalleryController.php
modules/group/controllers/EventsController.php
And another question. I need group object in all actions controllers under group module and i dont want to write the following code in all controllers
$group = Group::model()->findByPk($_GET['idgroup']);
The url's like:
/group/<idgroup>/forum/<idforum>
/group/<idgroup>/gallery/<idgallery>
What is the right way to do this?
Is this a correct way of structuring
groups?
The answer depends on a couple things:
Are you pulling in forum, gallery and events modules from a 3rd party?
Do forum, gallery and events contain their own models, views and controllers?
Do you plan on making forum, gallery and events stand-alone modules that others can use in their Yii applications?
If any of the above, then yes, it would make sense to put them into their own modules. Otherwise, adding them to the group module would be preferred.
According to Yii documentation:
A module is a self-contained software
unit that consists of models, views,
controllers and other supporting
components.
In regards to the adding the group object to all controller actions, generally I'll add a base Controller class for my module (something like /group/components/GroupController.php) and extend that controller rather than the base one in all of my controllers. GroupController should extend Yii's CController and contain the group object that you want to have accessible from your module's controllers.
Related
I wanted to see how you all handle this. In our Ext.application file we have our, models, stores and controllers config arrays defined with all the models, stores in controllers in our application. Only thing is we have so many that its really unruly to keep defining each one in this file its just becoming to bloated.
Does anyone have any recommendation on how to handle such an issue. We though about just make a javascript file the just define our arrays. Then just including each array in the config object as follows:
Ext.application({
models : MyApp.util.getModelsArray(),
controllers: MyApp.util.getControllersArray(),
stores: MyApp.util.getStoresArray()
})
Is there a better way, or is what a proposed above good?
I assume you are aware that you don't have to include all models and stores as application configs; models and stores (and views) given as configs to a controller will be loaded by the controller (when the controller is loaded by the application or dynamically). In fact, Ext.app.application inherits from the Ext.app.controller. So when you define controllers in your application, their stores and models will be loaded as well.
Considering encapsulation, reusability and proper object-oriented thinking, you should really define the stores, models and views in the controllers; and in your application only include models and stores your application methods need.
Then, how many controllers do you have? If you really have 'so many' you may want to consider only including essential ones (ie, ones that are required straight away when your application loads) and leave all other controllers to be loaded on demand. You can see how it's done in this SO answer.
I am having a very difficult time finding the answer to this. I want to create a custom class (this I know how to do) and have it get instantiated--one instance--that is globally accessible from within my application. I am looking to centralize and abstract some code and use this globally-available object as an interface. I can't believe how weird this is to figure out.
I need to have models, etc., available from within this object.
Help is appreciated.
I am running Rails 3.2.8.
Any model that you put in app/models will be autoloaded by Rails, so you can stick a custom model there.
The class will be available throughout your app, so whether you can just use class methods or not is up to you. If you want it to be a singleton, see this helpful article.
Lastly, if you need the model to instantiate in some specific way, just put it in an initializer. Any file in config/initializers will be run once as the app boots up.
You probably want a Singleton...
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/singleton/rdoc/Singleton.html
However, that will only be available to that apps process. If you run multiple app servers (ie. multiple thin instances or Passenger) each will have it's own instance.
If you need something truly global you'll have to look into other options.
I have a lot of functions that I need to access in my controllers and views.
Where is the best place to put these functions?
Functions used for view formatting across the whole application belong in the ApplicationHelper, however those helper functions should not perform any controller logic of their own. In other words, they should not be responsible for invoking instances of models or doing anything much with them.
Functions that do invoke models, or functions that supply objects or data that will be used by other controllers, and act on them are more appropriately stored in the ApplicationController. These would be functions like those needed to access application-wide login state in other controllers, for example.
the best place for such commonly used functions is application_controller.rb. I have used it to put code for checking if a user is logged in to determine if he/she needs to log in again.
I want to refer to $this->Model-> ... in the controller. But I want to make the functions generic, so how can I use do that dynamically? I tried $this->$modelname but of course that didn't work.
The CRUD functions will be generic to all models and thus all controllers, with overriding in a couple of cases.
EXAMPLE: Two controllers, one for each model -- Letter and Email. There is letter controller and email controller. Each has CRUD functions. The views are essentially identical, except the models track different information for each (e.g., Letter with send_method). The only thing that varies between them is the fields. I have automated that part, but the controllers are essentially the same thing as one another except for a few minor variations. I want to have a parent class and have it use the model name of the particular model, so I don't have to keep making changes to every controller every time I make a change. But in some instances I need to refer to $this->Model-> ... and I don't know how to do that.
Commplete rewrite based on clarification of OP
At the top of letters_controller.php add:
$this->defaultModel = 'Letter';
And in emails_controller.php add:
$this->defaultModel = 'Email';
In either controller, to reference the model, call
$this->{$this->defaultModel}->function();
Sounds like you're trying to re-invent the wheel: have you checked out the CakePHP Scaffolding section?
This is a complicated question with many possible answers, so I'll break down my situation into simple bullet points to help narrow down the solution:
My Rails App Has the Following 'Objects'
Author
Feed
Update
FeedTypes
The Objects are Related Like So:
Authors can have 1 or more Feeds
Feeds can have one or more Updates
A Feed has one feedType
Example Setup:
Author: Levi Hackwith
Feed: view-source:http://www.twitter.com/statuses/user_timeline/opnsrce.xml
FeedType: Twitter
Update: The tweets inside the Feed
My problem and My Questions:
Problem:
I need to parse the above-mentioned feed and store each tweet in the updates table. To parse the feed, I was thinking of writing a custom Feed class which would get inherited by TwitterFeed, FacebookFeed, TumblrFeed, etc.
However, I'm not sure if this is the 'Best Practice' for solving this kind of problem.
Questions:
When is it appropriate to develop a custom class to perform an action in RoR (as opposed to going through the Model or Controller)?
If this situation does not call for a custom class, which element should I apply the parsing logic to? The model or the controller?
If this is an appropriate situation for a custom class, where in my rails application should I store it (in other words, what's the right 'convention')?
You are probably going to have a background task invoked from time-to-time to check all the feeds, fetch new updates and store those in database. This task is completely separate from controllers and it should be possible to invoke it without any controller logic.
Your abstraction looks fine. You can further have something like XmlFeed < Feed if several feeds share a common XML structure.
1) Controllers should talk to database/models and pass relevant data to the view to render. Everything else should be either in a model, helper or library.
2) Are you asking where the parsing logic belongs to? In MVC, I think this would belong under the Model and/or a helper class, but definitely not the controller.. it's not its responsibility.
3) Classes holding data go into app/models. Classes that have nothing to do with holding data, go into the lib directory.
Don't shy away from using a custom class if it's appropriate. If you need another a class, then add one, the fact you are using rails is not relevant to that decision.