Programming Ruby on Rails: active record classify constants initialize load models rake Ruby on Rails
by bseanvt
leave a comment
Load All ActiveRecord::Base Model Classes in Rails Application
Here is a simple rake task which will instantiate all of your Active Record models, provided that they are located in the RAILS_ROOT/app/models directory. Interestingly, all plugin models are instantiated by default when you run the task, for instance, if you are using the Acts As Taggable On plugin, you have access to Tag, Tagging without having to include the plugin models directory path to the task.
namespace :load_ar do
desc "load up all active record models"
task :models => :environment do
models = ActiveRecord::Base.send(:subclasses)
Dir["#{RAILS_ROOT}/app/models/*"].each do |file|
model = File.basename(file, ".*").classify
models << model unless models.include?(model)
end
end
end
If your’re in the console, you can get all the load paths for your Active Record models with the following from the API.
Rails.configuration.load_paths.each do |path| p path end
How Beautiful is Ruby?
Working with Ruby and in particular Rails, it’s easy to take the beauty inherent in the language for granted. I mean look at this code. If you read it aloud to yourself, it reads like an english sentence that any non programmer can understand.
Forum.categories.map do |category| link_to category.name, category end.to_sentence
Forum categories map do category, link to category name, the category, end and convert to a sentence. The code returns a an english sentence as well.
Fish, French Bread, Coffee and Hamburgers with each linked to the correct resource as well!
Posts: configuration dreamhost freeze gems MySQL port Rails unpack
by bseanvt
leave a comment
Deploying to Dreamhost
Remember to include the host declaration in the database.yml file when you deploy to Dreamhost. Dreamhost does not use “localhost” which is typically the default setting when using the mysql adapter and developing locally or even on a small site.
At least for me, when I ported a Rails app to Dreamhost, this was the only “Gotcha”, because my log files were not reporting any errors and were instead serving the 500 something went wrong file.
A sample config/database.yml file
production: adapter: mysql username: youruser password: yourpasswd database: ror_production_db host: mysql.yourdomain.com
To port, I unpack my gems, if I haven’t already
rake gems:unpack
Then I freeze and package rails w/ my app just in case versions aren’t exact
rake rails:freeze:gems
Then I upload to Dreamhost!
oh russian text, i thought you had something valuable to share…
but no, you were only clever enough to use your native tongue in an attempt to fool me into publishing your funny, spam comment! thanks to google.com/translate, you were foiled in your plot to usurp traffic from my site
Programming Wordpress php: chris pearson gpl legality matt mullenweg mixergy php license thesis Wordpress
by bseanvt
leave a comment
Matt Mullenweg is Wrong. Thesis Should Carry Whatever License It Wants.
There is a debate underway about the legality of Thesis, a popular, paid for WordPress theme, using a different license than the one WordPress uses, which is the GPL. Matt Mullenweg, founder of WordPress, contends that all themes for the platform should inherit the WordPress license and should be considered a part of the platform. Chris Pearson, creator of Thesis, obviously has a different opinion. There have been many heated debates over the topic, but I haven’t heard an argument by either side that is compelling one way or the other.
My opinion, which I obviously think is compelling, though is not in any way rooted in any facet of the law or articulated in any legalese whatsoever, is that Matt Mullenweg is wrong. I love what Matt has done. I use and love the WordPress platform. I also think that his position is noble and rooted in the greater good for the WordPress community and open source in general.
I think what has been overlooked in his argument is an assumption we have about software, namely how it is supposed to work and what defines working software in relation to a pluggable software stack, such as WordPress. Technically, I could create an empty my-new-index.php file, place it inside the WordPress directory structure, and I have extended the platform. According to Matt, my software which does nothing, must inherit the WordPress license. Now if I want to sell (or redistribute) this do nothing piece of software for $50.00 (available upon request) I must also sell it with the GPL license. So technically, any piece of PHP code can placed inside of the WordPress directory structure and therefore, all PHP code must also carry the WordPress license!?!
There is obviously some rational limit and discretion available to us so that this sort of thing won’t ever happen! We could say because the Thesis theme makes use of the WordPress API, it must be intended for use with WordPress. However, the WordPress API is a bunch of PHP functions and WordPress itself uses the PHP API, which carries a different license (The PHP License). Which one takes precedence (if they were conflicting) ? Do all PHP applications, because they all must, by definition, use the PHP API, have to inherit the PHP License? This would be ridiculous and I’ve never seen a PHP app with the PHP license, so clearly using an API can’t include licensing requirements, so long as you aren’t modifying and redistributing the original API itself. For example, if I use a PEAR class, I don’t have to inherit the license for all of my work, I just can’t change the license of that class and or modify the original code without making it available again. Thesis doesn’t (to my knowledge) modify or extend any of the WordPress core. It provides interface enhancements, outputted HTML generated not by WordPress, but by Thesis. WordPress just consumes the Thesis software because it exists in a certain location, placed by someone into the WordPress directory structure.
This is the nature of pluggable systems, or software stacks. They are meant to be interoperable with other pieces of software whose parts and purposes are not known ahead of time. This covers a lot of space, space that isn’t even in existence and which it would, if Matt were correct, claim licensing rights to, in the event “it” comes into existence and “works” with said piece of software. Clearly Thesis “works” with WordPress. But so does a blank file. I intend for that file to do nothing, and it performs (quite efficiently) that task!
At the end of the day, it is only because Thesis is marketed to the WordPress community and is intended to be used with the WordPress platform, that there is an argument about how the software should be licensed. But Thesis is not sold bundled with WordPress, nor is anyone who purchases it required to use it with WordPress. For all intents and purposes, it is a stand alone piece of code, that well, does nothing important or worthwhile. Can you really license functionality that may or may not exist depending on how the purchaser decides to implement their purchase, just like my overpriced(though well worth the investment), blank PHP file could be used in any PHP application?
I have a lot of questions (I haven’t read all the licenses either, so I may be in total error
. Though from what I’ve read and heard so far, it is only because we have an expectation of how the software will work when it is in the WordPress directory structure that there is a conflict with how Chris has licensed Thesis. This expectation is set by the marketing of the software, not necessarily the software itself. Aside from Trademark infringement, does the language used to sell a product carry over into the the actual license the code must carry? This wouldn’t make sense. Say for instance, “Runs on Windows”, would require another license too?
I think we’re going to see a lot more arguments over licensing conflicts like what we’re seeing with WordPress/Thesis. Software is the biggest it has ever been. Open Source software is integrated into the business world like never before and business models are built on top of many of the various projects which are out there, “free” to use. As this space expands, no doubt more and more gray areas will as well.
On a practical note, what would stop Chris from writing a simple wrapper around the WordPress API his files use, license this with the GPL, use his wrapper functions instead from inside Thesis and then keep the current Thesis license?
Programming Wordpress: data export import links lynx opml xml
by bseanvt
leave a comment
How to Import/Export Your WordPress Blogroll… er, Your Links
It’s not immediately apparent how to import/export the links in your WordPress blogroll. One would expect that the import/export tool, used to backup/restore WordPress posts and pages would handle this functionality as well. But the import/export tool has many options. You need to select the Blogroll import option (located near the top of the list) and provide the tool with either the destination of a valid OPML XML url, or upload a valid OPML XML file. If you’re running on WordPress, this can be found by appending a “/wp-links-opml.php” to the URL of your domain. For example, http://seanbehan.com/wp-links-opml.php will show you my blogroll in XML. If your running out of a subdirectory, don’t forget to include that path in the URL.
If for some reason you’re running a development website on your localhost, obviously the live, remote site will not be able to contact your localhost for the data. You will have to upload the XML file to the remote site. The WordPress export tool does not provide the means to generate a the OPML XML file for you. You will have to visit the /wp-links-opml.php file and download the contents yourself, but this depends on how your browser handles XML files. Firefox will show you the raw XML, however, Safari will show you the contents of the XML nodes… which will most likely be only the name of your website. You’ll need to go to “View Source” option to see the actual XML code.Copy and past the XML to a file with the .xml file extension somewhere on your hard drive. Visit the import > blogroll tool and upload the file!
A simple way to download the file contents is to use the command line web browser Lynx. http://en.wikipedia.org/wiki/Lynx_%28web_browser%29
Just give Lynx the destination URL and the file to write to (it doesn’t have to exist) and then you can upload this file
<pre>
lynx -source URL > my-links.xml
</pre>
More info on lynx available here: http://kb.iu.edu/data/aczi.html
Programming moodle php: context course has_capbility moodle php teacher
by bseanvt
1 comment
How to Check if the Current Logged In User can Edit a Course in MOODLE Using the Has_capability Function
After an hour of fruitless searching through the docs and forums, I finally found an answer to my simple question, of course buried in the depths of the shark infested, murky water that is the Moodle code base .
How do I check if a user is a course creator for a given course? I’m still not 100% sure, the access control policy seems needlessly confusing on first glance and even after reading the heady and mostly pointless, expose on how roles/perms are calculated (Available Here: http://docs.moodle.org/en/How_permissions_are_calculated ) If you notice the section titled “Some Practical Examples” is 100% empty! (I’ve added my code to the wiki so not 100% empty any longer) The most useful part of the docs are completely empty! Instead boatloads of time went into a treatise on the internals and lovely graphs like this one 
But you could always hop into the forum dedicated just to this chunk of the code base (Available here: http://moodle.org/mod/forum/view.php?f=941&page=0) and search through the 500 topics w/ 5000+(i didn’t actually count) nested discussions (including version 1.7 of moodle as well).
No, instead, ACK came to the rescue and I found a snippet that performed the task.
Without further ado
$context = get_context_instance(CONTEXT_COURSE, $course->id);
if (has_capability('moodle/course:update', $coursecontext)) {
/** do stuff here */
}
There it is. That could have saved me an hour if mentioned in the docs!
Moodle's Most Important Function Gets No Attention
Arguably the most important function in the Moodle API, is the create_course function. One would think… after all Moodle is an LMS. Courses are the bread and butter for course management platforms, right? Taking a look at the function in the course/lib.php file reveals almost nothing about the function! WTF? … btw, the function starts at line 3260
/**
* Create a course and either return a $course object or false
*
* @param object $data - all the data needed for an entry in the 'course' table
*/
function create_course($data) {
global $CFG, $USER;
/** omitted for your sanity */
}
Wow, I think I know less about this function now than before I went digging into the code. How do I assign a teacher to my new course, how do I populate the data object it takes as a parameter (is there a course setup function)? Since courses aren’t really objects in Moodle (just glorified arrays), am I just supposed to set them up by hand… $object->param = val? Are there default values and/or required attributes that need to be set, otherwise my program is going to come to a halt? What about student enrollments? No example to help for how one might actually use this function? Do I have to really read through the code, starting at around line 3260, then jump between the rest of this monstrous 4000 line piece of spaghetti coded file and try and make sense of it works (somehow).
How is it possible that the most important function (since the core problem Moodle solves is creating courses) in the entire application has no meaningful documentation? I think I have an answer. The entire code base is soooo convoluted and a giant waste of CPU cycles, that it would too embarrassing to actually document how it even works.
Linux Programming: compression exclude photoshop psd zip
by bseanvt
leave a comment
Recursively Zip Up a Directory while Excluding Certain Files Based on File Extension Type
In the example below, I’m going to zip up a directory that includes images in both PNG and PSD file formats. However, I want to exclude the PSDs because they are huge!
zip -r my-compressed-dir-without-psd.zip directory-to-zip -x '*.psd'
Linux Programming: bzip2 commands compression tar uncompress unix
by bseanvt
leave a comment
Uncompress A Bz2 File Using Tar Command
Uncompress a bz2 file using tar command
tar --use-compress-program bzip2 -xvf your-file-to-uncompress.tar.bz2
@source http://www.kde.gr.jp/help/doc/kdebase/doc/khelpcenter/faq/HTML/bzip2.html