nighty commit?
This commit is contained in:
parent
cb2a2f9c1d
commit
144c021616
@ -214,7 +214,7 @@ abstract class Base extends ActionController
|
|||||||
|
|
||||||
public function action_name()
|
public function action_name()
|
||||||
{
|
{
|
||||||
return Rails::services()->get('inflector')->camelize(Rails::application()->dispatcher()->router()->route()->action, true);
|
return Rails::services()->get('inflector')->camelize(Rails::application()->dispatcher()->router()->route()->action, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function run_request_action()
|
public function run_request_action()
|
||||||
|
@ -40,6 +40,7 @@ class ViewHelpers
|
|||||||
return $helper;
|
return $helper;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ use Rails\ActiveModel;
|
|||||||
abstract class Base
|
abstract class Base
|
||||||
{
|
{
|
||||||
use Methods\CounterMethods, Methods\RelationMethods, Methods\ScopingMethods,
|
use Methods\CounterMethods, Methods\RelationMethods, Methods\ScopingMethods,
|
||||||
Methods\AttributeMethods, Methods\ModelSchemaMethods;
|
Methods\AttributeMethods, Methods\ModelSchemaMethods, Methods\AssociationMethods;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ActiveRecord_Registry instance.
|
* ActiveRecord_Registry instance.
|
||||||
@ -40,12 +40,6 @@ abstract class Base
|
|||||||
*/
|
*/
|
||||||
private $isNewRecord = true;
|
private $isNewRecord = true;
|
||||||
|
|
||||||
/**
|
|
||||||
* An array where loaded associations will
|
|
||||||
* be stored.
|
|
||||||
*/
|
|
||||||
private $loadedAssociations = [];
|
|
||||||
|
|
||||||
static public function __callStatic($method, $params)
|
static public function __callStatic($method, $params)
|
||||||
{
|
{
|
||||||
if ($rel = static::scope($method, $params))
|
if ($rel = static::scope($method, $params))
|
||||||
@ -136,6 +130,11 @@ abstract class Base
|
|||||||
return self::connection()->selectValue('SELECT MAX(' . $attr . ') FROM ' . static::tableName());
|
return self::connection()->selectValue('SELECT MAX(' . $attr . ') FROM ' . static::tableName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static public function count()
|
||||||
|
{
|
||||||
|
return self::connection()->selectValue('SELECT COUNT(*) FROM `' . self::tableName() . '`');
|
||||||
|
}
|
||||||
|
|
||||||
static public function I18n()
|
static public function I18n()
|
||||||
{
|
{
|
||||||
return Rails::application()->I18n();
|
return Rails::application()->I18n();
|
||||||
@ -524,22 +523,12 @@ abstract class Base
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getAssociation($name)
|
|
||||||
{
|
|
||||||
if (isset($this->loadedAssociations[$name])) {
|
|
||||||
return $this->loadedAssociations[$name];
|
|
||||||
} elseif ($assoc = $this->get_association_data($name)) {
|
|
||||||
$model = $this->_load_association($name, $assoc[0], $assoc[1]);
|
|
||||||
$this->loadedAssociations[$name] = $model;
|
|
||||||
return $this->loadedAssociations[$name];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ***************************
|
* ***************************
|
||||||
* Default protected methods {
|
* Default protected methods {
|
||||||
* ***************************
|
* ***************************
|
||||||
* attrAccessible and attrProtected can be found in Base\Methods\AttributeMethods.
|
* attrAccessible and attrProtected can be found in Base\Methods\AttributeMethods.
|
||||||
|
* associations can be found in Base\Methods\AssociationMethods
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -550,11 +539,6 @@ abstract class Base
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function associations()
|
|
||||||
{
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Example:
|
* Example:
|
||||||
*
|
*
|
||||||
@ -648,14 +632,6 @@ abstract class Base
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array|Closure $params - Additional parameters to customize the query for the association
|
|
||||||
*/
|
|
||||||
private function _load_association($prop, $type, $params)
|
|
||||||
{
|
|
||||||
return $this->{'_find_' . $type}($prop, $params);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function _get_parents_callbacks($callback_name)
|
private function _get_parents_callbacks($callback_name)
|
||||||
{
|
{
|
||||||
$all_callbacks = array();
|
$all_callbacks = array();
|
||||||
@ -673,17 +649,6 @@ abstract class Base
|
|||||||
return $all_callbacks;
|
return $all_callbacks;
|
||||||
}
|
}
|
||||||
|
|
||||||
# Returns association property names.
|
|
||||||
private function _associations_names()
|
|
||||||
{
|
|
||||||
$associations = array();
|
|
||||||
foreach ($this->associations() as $assocs) {
|
|
||||||
foreach ($assocs as $k => $v)
|
|
||||||
$associations[] = is_int($k) ? $v : $k;
|
|
||||||
}
|
|
||||||
return $associations;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
* @see validations()
|
* @see validations()
|
||||||
@ -937,25 +902,6 @@ abstract class Base
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function get_association_data($prop)
|
|
||||||
{
|
|
||||||
if ($assocs = $this->associations()) {
|
|
||||||
foreach ($assocs as $type => $assoc) {
|
|
||||||
foreach ($assoc as $name => $params) {
|
|
||||||
if (is_int($name)) {
|
|
||||||
$name = $params;
|
|
||||||
$params = array();
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($name == $prop) {
|
|
||||||
return array($type, $params);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function _register()
|
private function _register()
|
||||||
{
|
{
|
||||||
self::_registry()->register($this);
|
self::_registry()->register($this);
|
||||||
|
@ -1 +1,77 @@
|
|||||||
<?php
|
<?php
|
||||||
|
namespace Rails\ActiveRecord\Base\Methods;
|
||||||
|
|
||||||
|
use Rails\ActiveRecord\Exception;
|
||||||
|
|
||||||
|
trait AssociationMethods
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* An array where loaded associations will
|
||||||
|
* be stored.
|
||||||
|
*/
|
||||||
|
private $loadedAssociations = [];
|
||||||
|
|
||||||
|
protected function associations()
|
||||||
|
{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getAssociation($name)
|
||||||
|
{
|
||||||
|
if (isset($this->loadedAssociations[$name])) {
|
||||||
|
return $this->loadedAssociations[$name];
|
||||||
|
} elseif ($assoc = $this->get_association_data($name)) {
|
||||||
|
$model = $this->_load_association($name, $assoc[0], $assoc[1]);
|
||||||
|
$this->loadedAssociations[$name] = $model;
|
||||||
|
return $this->loadedAssociations[$name];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function setAssociation($name, $object)
|
||||||
|
{
|
||||||
|
if (!in_array($name, $this->_associations_names())) {
|
||||||
|
throw new Exception\RuntimeException(
|
||||||
|
sprintf("Tried to set unknown association: %s", $name)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
$this->loadedAssociations[$name] = $object;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Returns association property names.
|
||||||
|
private function _associations_names()
|
||||||
|
{
|
||||||
|
$associations = array();
|
||||||
|
foreach ($this->associations() as $assocs) {
|
||||||
|
foreach ($assocs as $k => $v)
|
||||||
|
$associations[] = is_int($k) ? $v : $k;
|
||||||
|
}
|
||||||
|
return $associations;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array|Closure $params - Additional parameters to customize the query for the association
|
||||||
|
*/
|
||||||
|
private function _load_association($prop, $type, $params)
|
||||||
|
{
|
||||||
|
return $this->{'_find_' . $type}($prop, $params);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function get_association_data($prop)
|
||||||
|
{
|
||||||
|
if ($assocs = $this->associations()) {
|
||||||
|
foreach ($assocs as $type => $assoc) {
|
||||||
|
foreach ($assoc as $name => $params) {
|
||||||
|
if (is_int($name)) {
|
||||||
|
$name = $params;
|
||||||
|
$params = array();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($name == $prop) {
|
||||||
|
return array($type, $params);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -170,7 +170,9 @@ trait RelationMethods
|
|||||||
*/
|
*/
|
||||||
private function _find_has_many($prop, $params)
|
private function _find_has_many($prop, $params)
|
||||||
{
|
{
|
||||||
empty($params['class_name']) && $params['class_name'] = rtrim(ucfirst($prop), 's');
|
$inflector = \Rails::services()->get('inflector');
|
||||||
|
|
||||||
|
empty($params['class_name']) && $params['class_name'] = $inflector->camelize($inflector->singularize($prop));
|
||||||
|
|
||||||
$builder = new Association($params, $this);
|
$builder = new Association($params, $this);
|
||||||
$builder->build_query();
|
$builder->build_query();
|
||||||
|
@ -27,8 +27,9 @@ class Association extends AbstractRelation
|
|||||||
$params = $this->params;
|
$params = $this->params;
|
||||||
|
|
||||||
if (empty($params['foreign_key'])) {
|
if (empty($params['foreign_key'])) {
|
||||||
|
$inflector = \Rails::services()->get('inflector');
|
||||||
$cn = get_class($this->parent_model);
|
$cn = get_class($this->parent_model);
|
||||||
$params['foreign_key'] = substr($cn::tableName(), 0, -1).'_id';
|
$params['foreign_key'] = $inflector->singularize($cn::tableName()).'_id';
|
||||||
}
|
}
|
||||||
|
|
||||||
$query = new Relation($params['class_name'], $params['class_name']::tableName());
|
$query = new Relation($params['class_name'], $params['class_name']::tableName());
|
||||||
|
@ -147,7 +147,7 @@ class Inflector
|
|||||||
|
|
||||||
public function classify($tableName)
|
public function classify($tableName)
|
||||||
{
|
{
|
||||||
return $this->camelize($this->singuralize(preg_replace('/.*\./', '', $tableName)));
|
return $this->camelize($this->singularize(preg_replace('/.*\./', '', $tableName)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function ordinal($number)
|
public function ordinal($number)
|
||||||
|
Loading…
Reference in New Issue
Block a user